【发布时间】:2021-06-23 00:31:39
【问题描述】:
我正在尝试使用鼠标中键在 X 轴和 Y 轴上旋转我的第三人称玩家游戏对象的摄像机角度。
我在下面有这段代码。
public class CameraControl : MonoBehaviour
{
public Transform player;
public Vector3 offset;
public float pitch = 2f;
private float turnSpeed = 5f;
private float currentZoom = 10f;
void LateUpdate()
{
if (Input.GetMouseButton(2))
{
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
offset = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;
}
transform.position = player.position - offset * currentZoom;
transform.LookAt(player.position + Vector3.up * pitch);
}
}
单轴旋转似乎按预期工作,我可以沿 X 轴或 Y 轴旋转而没有任何问题,但是当它们混合在一起(同时在两个方向上移动)时,有些东西似乎会破裂并且旋转似乎也不起作用。就好像它在途中失去了沿 Y 轴移动的能力。
另一个问题是我不太确定如何限制 Y 轴,以使旋转不会低于地面或高于和围绕玩家的头部。我考虑过使用 Mathf.Clamp,但在我的代码中设置最小值和最大值似乎太成问题了。
【问题讨论】: