diff --git a/Assets/Behaviours/CameraBehaviour.cs b/Assets/Behaviours/CameraBehaviour.cs index e5a8fea..775ec22 100644 --- a/Assets/Behaviours/CameraBehaviour.cs +++ b/Assets/Behaviours/CameraBehaviour.cs @@ -7,15 +7,14 @@ public class CameraBehaviour : MonoBehaviour private const int MouseWheelSensitivity = 1; //滚轮灵敏度设置 private const int MouseZoomMin = 1; //相机距离最小值 private const int MouseZoomMax = 20; //相机距离最大值 - private const float XSpeed = 250.0f; - private const float YSpeed = 120.0f; + private const float XSpeed = 120.0f; + private const float YSpeed = 250.0f; private const int YMinLimit = -360; private const int YMaxLimit = 360; private float _eulerX; //存储相机的euler角 private float _eulerY; //存储相机的euler角 - - private float _distance = 5; //相机和target之间的距离,因为相机的Z轴总是指向target,也就是相机z轴方向上的距离 + private float _distance; //相机和target之间的距离,因为相机的Z轴总是指向target,也就是相机z轴方向上的距离 private Vector3 _targetOnScreenPosition; //目标的屏幕坐标,第三个值为z轴距离 private Quaternion _cameraRotation; //存储相机的姿态四元数 private Vector3 _targetPosition; //target的位置 @@ -31,11 +30,13 @@ private void Start() { //这里就是设置一下初始的相机视角以及一些其他变量,这里的x和y。。。是和下面getAxis的mouse x与mouse y对应 var angles = transform.eulerAngles; - _eulerX = angles.y; - _eulerY = angles.x; - _targetPosition = target.position; - //_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); - _cameraRotation = Quaternion.Euler(_eulerY, _eulerX, 0); + _eulerX = angles.x; + _eulerY = angles.y; + var position = target.position; + _targetPosition = position; + _cameraRotation = Quaternion.Euler(_eulerX, _eulerY, 0); + _distance = Vector3.Distance(position, transform.position); + // 引入中间变量 提高代码效率 var transform1 = transform; //设置相机姿态 @@ -52,13 +53,12 @@ private void Update() //鼠标右键旋转功能 if (Input.GetMouseButton(1)) { - _eulerX += Input.GetAxis("Mouse X") * XSpeed * 0.02f; - _eulerY -= Input.GetAxis("Mouse Y") * YSpeed * 0.02f; + // 鼠标控制需要反转X、Y轴 + _eulerX += Input.GetAxis("Mouse Y") * XSpeed * 0.02f; + _eulerX = ClampAngle(_eulerX, YMinLimit, YMaxLimit); + _eulerY -= Input.GetAxis("Mouse X") * YSpeed * 0.02f; - _eulerY = ClampAngle(_eulerY, YMinLimit, YMaxLimit); - - //_cameraRotation = Quaternion.Euler(_eulerY + 60, _eulerX, 0); - _cameraRotation = Quaternion.Euler(_eulerY, _eulerX, 0); + _cameraRotation = Quaternion.Euler(_eulerX, _eulerY, 0); var position = _cameraRotation * new Vector3(0.0f, 0.0f, -_distance) + _targetPosition; // 引入中间变量 提高代码效率 @@ -66,7 +66,8 @@ private void Update() transform1.rotation = _cameraRotation; transform1.position = position; } - else if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能 + + if (Input.GetAxis("Mouse ScrollWheel") != 0) //鼠标滚轮缩放功能 { if (_distance is >= MouseZoomMin and <= MouseZoomMax) {