简体   繁体   English

如何修复我的鼠标而不是在 X 轴上旋转相机?

[英]How can I fix my mouse not rotation the camera on the X axis?

So my problem is with my controllable first person camera, which is currently just a camera with a collider, Character Controller, and this script on it.所以我的问题是我的可控第一人称相机,它目前只是一个带有碰撞器、角色控制器和这个脚本的相机。 When I enter the game, the MouseX values change in the insector, but the camera itself appear to be locked, I can only move it up and down.当我进入游戏时,昆虫中的MouseX值发生了变化,但相机本身似乎被锁定了,我只能上下移动它。 This is just a small amount of the main code, but I think it contains enough to show the problem.这只是一小部分主要代码,但我认为它包含的内容足以说明问题。

[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);
}

You will work better if you rotate by using the transform.Rotate() method instead of modifying the Quaternion, for example:如果您使用 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);
}

And if you want to limit the max angle of your camera at (-90, 90) you can accumulate the m_MouseY on another variable, and rotate each way (+/-) only when that variable is inside of the correct inverval, for example:如果您想将相机的最大角度限制在 (-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM