[英]How can I fix my mouse not rotation the camera on the X axis?
所以我的問題是我的可控第一人稱相機,它目前只是一個帶有碰撞器、角色控制器和這個腳本的相機。 當我進入游戲時,昆蟲中的MouseX值發生了變化,但相機本身似乎被鎖定了,我只能上下移動它。 這只是一小部分主要代碼,但我認為它包含的內容足以說明問題。
[SerializeField]
private Camera m_Camera;
[SerializeField]
private CharacterController m_CharacterController;
[SerializeField]
private float m_MouseX;
[SerializeField]
private float m_MouseY;
void Update()
{
Rotate();
Movement();
}
private void Rotate()
{
// Receive mouse input and modifies
m_MouseX += Input.GetAxis("Mouse X") * m_LookSensitivity;
m_MouseY += Input.GetAxis("Mouse Y") * m_LookSensitivity;
// Keep mouseY between -90 and +90
m_MouseY = Mathf.Clamp(m_MouseY, -90.0f, 90.0f);
// Rotate the player object around the y-axis
transform.localRotation = Quaternion.Euler(Vector3.up * m_MouseX);
// Rotate the camera around the x-axis
m_Camera.transform.localRotation = Quaternion.Euler(Vector3.left * m_MouseY);
}
如果您使用 transform.Rotate() 方法而不是修改四元數進行旋轉,您會工作得更好,例如:
private void Rotate()
{
// Receive mouse input and modifies
m_MouseX += Input.GetAxis("Mouse X") * m_LookSensitivity;
m_MouseY += Input.GetAxis("Mouse Y") * m_LookSensitivity;
// Rotate the player object around the y-axis
transform.Rotate(Vector3.up * m_MouseX);
// Rotate the camera around the x-axis
m_Camera.transform.Rotate(Vector3.left * m_MouseY);
}
如果您想將相機的最大角度限制在 (-90, 90),您可以將 m_MouseY 累積到另一個變量上,並且僅當該變量在正確的 inverval 范圍內時才以單向 (+/-) 旋轉,例如:
totalY += m_MouseY;
if (totalY < 90 && m_MouseY > 0)
m_Camera.transform.Rotate(Vector3.left * m_MouseY);
if (totalY > -90 && m_MouseY < 0)
m_Camera.transform.Rotate(Vector3.left * m_MouseY);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.