修复相机跳跃问题

This commit is contained in:
shenjianxing 2025-01-13 11:55:37 +08:00
parent d8b56b667a
commit 593fe9e0bf

View File

@ -22,6 +22,7 @@ public class FreeCameraController : MonoBehaviour
private float yRotation = 0.0f; private float yRotation = 0.0f;
public bool isMov = true; public bool isMov = true;
public bool isRot = true; public bool isRot = true;
private void Awake() private void Awake()
{ {
instance = this; instance = this;
@ -32,7 +33,7 @@ public class FreeCameraController : MonoBehaviour
void Update() void Update()
{ {
if (isMov == true) if (isMov)
{ {
// 相机移动 // 相机移动
float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime; float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
@ -58,7 +59,6 @@ public class FreeCameraController : MonoBehaviour
{ {
// 相机旋转 // 相机旋转
Vector3 mouseDelta = Input.mousePosition - lastMousePosition; // 反转了鼠标差值 Vector3 mouseDelta = Input.mousePosition - lastMousePosition; // 反转了鼠标差值
lastMousePosition = Input.mousePosition; lastMousePosition = Input.mousePosition;
@ -68,8 +68,8 @@ public class FreeCameraController : MonoBehaviour
// 限制 X 轴旋转范围 // 限制 X 轴旋转范围
xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit); xRotation = Mathf.Clamp(xRotation, -xRotationLimit, xRotationLimit);
//// ÏÞÖÆ Y ÖáÐýת·¶Î§ // 限制 Y 轴旋转范围
//if (minRotationLimitY!=-1&&maxRotationLimitY!=-1) //if (minRotationLimitY != -1 && maxRotationLimitY != -1)
//{ //{
// yRotation = Mathf.Clamp(yRotation, minRotationLimitY, maxRotationLimitY); // yRotation = Mathf.Clamp(yRotation, minRotationLimitY, maxRotationLimitY);
//} //}
@ -77,10 +77,6 @@ public class FreeCameraController : MonoBehaviour
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0); transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
} }
} }
} }
// 公共方法:旋转相机到指定方向 // 公共方法:旋转相机到指定方向
@ -102,9 +98,27 @@ public class FreeCameraController : MonoBehaviour
Vector3 currentRotation = transform.eulerAngles; Vector3 currentRotation = transform.eulerAngles;
xRotation = currentRotation.x; xRotation = currentRotation.x;
yRotation = currentRotation.y; yRotation = currentRotation.y;
transform.eulerAngles = currentRotation;
//minRotationLimitY = currentRotation.y - yRotationLimit / 2; // 归一化 xRotation 和 yRotation
//maxRotationLimitY = currentRotation.y + yRotationLimit / 2; 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) public void SetLock(bool isMov, bool isRot)