【问题标题】:How can I fix my mouse not rotation the camera on the X axis?如何修复我的鼠标不在 X 轴上旋转相机?
【发布时间】:2020-02-21 00:43:41
【问题描述】:

所以我的问题在于我的可控第一人称相机,它目前只是一个带有碰撞器、角色控制器和这个脚本的相机。当我进入游戏时,昆虫中的 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);
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    如果你使用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);
    

    【讨论】:

    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 2022-11-29
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2017-04-22
    • 2016-09-22
    • 1970-01-01
    相关资源
    最近更新 更多