diff --git a/Assets/Scripts/FreeCameraController.cs b/Assets/Scripts/FreeCameraController.cs index 89c40a15..d0072685 100644 --- a/Assets/Scripts/FreeCameraController.cs +++ b/Assets/Scripts/FreeCameraController.cs @@ -22,6 +22,7 @@ public class FreeCameraController : MonoBehaviour private float yRotation = 0.0f; public bool isMov = true; public bool isRot = true; + private void Awake() { instance = this; @@ -32,7 +33,7 @@ public class FreeCameraController : MonoBehaviour void Update() { - if (isMov == true) + if (isMov) { // 相机移动 float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime; @@ -58,7 +59,6 @@ public class FreeCameraController : MonoBehaviour { // 相机旋转 - Vector3 mouseDelta = Input.mousePosition - lastMousePosition; // 反转了鼠标差值 lastMousePosition = Input.mousePosition; @@ -68,8 +68,8 @@ public class FreeCameraController : MonoBehaviour // 限制 X 轴旋转范围 xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit); - //// 限制 Y 轴旋转范围 - //if (minRotationLimitY!=-1&&maxRotationLimitY!=-1) + // 限制 Y 轴旋转范围 + //if (minRotationLimitY != -1 && maxRotationLimitY != -1) //{ // yRotation = Mathf.Clamp(yRotation, minRotationLimitY, maxRotationLimitY); //} @@ -77,10 +77,6 @@ public class FreeCameraController : MonoBehaviour transform.rotation = Quaternion.Euler(xRotation, yRotation, 0); } } - - - - } // 公共方法:旋转相机到指定方向 @@ -102,9 +98,27 @@ public class FreeCameraController : MonoBehaviour Vector3 currentRotation = transform.eulerAngles; xRotation = currentRotation.x; yRotation = currentRotation.y; - transform.eulerAngles = currentRotation; - //minRotationLimitY = currentRotation.y - yRotationLimit / 2; - //maxRotationLimitY = currentRotation.y + yRotationLimit / 2; + + // 归一化 xRotation 和 yRotation + if (xRotation > 180f) + { + xRotation -= 360f; + } + else if (xRotation < -180f) + { + xRotation += 360f; + } + + if (yRotation > 180f) + { + yRotation -= 360f; + } + else if (yRotation < -180f) + { + yRotation += 360f; + } + + transform.eulerAngles = new Vector3(xRotation, yRotation, 0); } public void SetLock(bool isMov, bool isRot)