Unity相机的旋转缩放 发表于 2017-10-30 | 分类于 Unity | 阅读次数: 摄像机的旋转,这里用的Rotate。摄像机的缩放有两种方法,一种是filedOfView的改变,一种是改变摄像机Z轴的位置。都是可以的,下面是代码: 1234567891011121314151617181920212223242526272829303132333435363738public float zoomSpeed = 10; //缩放速度public float rotateSpeed = 1; //旋转速度private float delta_z;private float delta_rotation_x, delta_rotation_y;void Update(){ //摄像机旋转 if (Input.GetMouseButton(0)) { delta_rotation_x = -Input.GetAxis("Mouse X") * rotateSpeed; delta_rotation_y = Input.GetAxis("Mouse Y") * rotateSpeed; transform.Rotate(0, delta_rotation_x, 0, Space.World); transform.Rotate(delta_rotation_y, 0, 0); } //摄像机缩放 if (Input.GetAxis("Mouse ScrollWheel") != 0) { delta_z = -Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;// //第一种方法,改变注摄像机的filedOfView// Camera.main.fieldOfView += delta_z;// if (Camera.main.fieldOfView < 10) Camera.main.fieldOfView = 10;// if (Camera.main.fieldOfView > 100) Camera.main.fieldOfView = 100; //第二种方法,改变摄像机的Z轴值 transform.Translate(0, 0, -delta_z); if (transform.localPosition.z > -2) { transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, -2); } if (transform.localPosition.z < -25) { transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, -25); } }} 坚持原创技术分享,您的支持将鼓励我继续创作! 打赏 微信支付 支付宝