我目前正在使用 Unity 游戏引擎创建一个 3D 平台游戏,我正在关注本教程系列以获得一些基本的玩家运动:https ://www.youtube.com/watch?v=h2d9Wc3Hhi0&list=PLiyfvmtjWC_V_H-VMGGAZi7n5E0gyhc37 。在我完成教程的运动部分后,我遇到了一个问题。每当我将鼠标移动到旋转阈值时,相机都会避开阈值并围绕 y 轴旋转。旋转后,鼠标输入反转。这是我的相机控制器代码:
using UnityEngine;
namespace Player
{
public class CameraController : MonoBehaviour
{
public Transform target;
public Transform orientation;
public Vector3 offset;
public bool useOffsetValues;
public float sensitivity;
public float maxViewAngle = 85;
public float minViewAngle = -75;
public bool invertY;
private void Start()
{
if (!useOffsetValues)
{
offset = target.position - transform.position;
}
orientation.position = target.position;
orientation.parent = null;
Cursor.lockState = CursorLockMode.Locked;
}
private void LateUpdate()
{
orientation.position = target.position;
float yaw = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime;
orientation.Rotate(0, yaw, 0);
float pitch = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime;
if (invertY) orientation.Rotate(pitch, 0, 0);
else orientation.Rotate(-pitch, 0, 0);
if (orientation.rotation.eulerAngles.x > maxViewAngle && orientation.rotation.eulerAngles.x < 180)
{
orientation.rotation = Quaternion.Euler(maxViewAngle, orientation.eulerAngles.y, 0);
}
if (orientation.rotation.eulerAngles.x < 360 + minViewAngle && orientation.rotation.eulerAngles.x > 180)
{
orientation.rotation = Quaternion.Euler(360 + minViewAngle, orientation.eulerAngles.y, 0);
}
float yAngle = orientation.eulerAngles.y;
float xAngle = orientation.eulerAngles.x;
Quaternion rot = Quaternion.Euler(xAngle, yAngle, 0);
transform.position = target.position - (rot * offset);
if (transform.position.y < target.position.y - 0.5f)
{
transform.position = new Vector3(transform.position.x, target.position.y - 0.5f, transform.position.z);
}
transform.LookAt(target);
}
}
}
任何帮助,将不胜感激!