首页 > 解决方案 > 无法让我的相机正确限制其旋转

问题描述

在我的游戏中,我想夹住玩家的摄像头,使其无法进行前空翻或后空翻。我想将 x 轴夹在 -75 和 50 之间,但它不起作用。

每次我在我的代码中添加一个夹子时,相机都不想将它的旋转从 0,0,0 移动到比 Input.GetAxisRaw 说的鼠标移动更远的地方。

我也尝试过将 if 语句用作手动钳位,但如果我切换极性,它要么保持在 0,0,0 不变,要么保持在 -75,0,0 不变。

我尝试更换相机,以防它与设置相关但没有任何变化。

我不想发布这个,因为它不应该这么难,但我已经花了好几天的时间,我没有选择;任何和所有的想法都非常感谢。

我使用 Visual Studio 作为我的编辑器。

using UnityEngine;

public class PlayerMove : MonoBehaviour {

    Rigidbody rb;

    public Camera cam;
    public Transform camTrans;

    Vector3 movement;
    Vector3 rotation;

    public float sensitivityX;
    public float sensitivityY;
    public float playerSpeed;
    public float jumpForce;
    float forward;
    float sideways;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
        forward = Input.GetAxisRaw("Vertical");
        sideways = Input.GetAxisRaw("Horizontal");
        movement = new Vector3 (forward, 0, -sideways).normalized;

        float _xRot = Input.GetAxisRaw("Mouse Y");
        float _yRot = Input.GetAxisRaw("Mouse X");

        rotation = new Vector3(0f, _yRot, 0f) * sensitivityX;

        float _jump = Input.GetAxisRaw("Jump");

        if (movement != Vector3.zero)
        {
            MovePlayer();
        }
        if (rotation != Vector3.zero && Input.GetAxisRaw("Fire2") != 0 || _xRot != 0 && Input.GetAxisRaw("Fire2") != 0)
        {
            Rotate(-_xRot);
        }
        if (_jump != 0f)
        {
            Jump();
        }
    }

    void MovePlayer()
    {
        float _playerSpeed;
        _playerSpeed = playerSpeed * 0.1f;
        transform.Translate(movement * _playerSpeed * Time.fixedDeltaTime, Space.Self);
    }

    void Jump()
    {
        if (IsGrounded())
        {
            rb.AddForce(new Vector3(0, 1 * jumpForce, 0), ForceMode.Impulse);
        }
    }

    void Rotate(float _camRot)
    {
        camTrans.Rotate(new Vector3(_camRot * sensitivityY, 0, 0));
        float _camPosX = camTrans.rotation.x;
        Mathf.Clamp(_camPosX, -75, 50);
        camTrans.rotation = Quaternion.Euler(new Vector3(_camPosX, 0, 0));
        rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation * sensitivityX));
    }

    bool IsGrounded()
    {
        RaycastHit hit;
        return Physics.Raycast(transform.position, Vector3.down, out hit, 1.001f);
    }
}

标签: c#unity3d

解决方案


Input.GetAxisRaw("Mouse Y");返回鼠标移动的单位数(参见Unity 文档 - Input.GetAxisRaw)。

当您移动鼠标时,脚本可以工作,但是因为在一些帧之后调用了函数中void Rotate()的每一帧;返回 0 然后.Update()Input.GetAxisRaw("Mouse Y")_camRot = 0

因此,camTrans.rotation = Quaternion.Euler(new Vector3(0, 0, 0));由于Update()每秒调用多次,我们认为相机始终保持在 0,0,0。

要限制旋转,您可以将代码更改为:

public class PlayerMove : MonoBehaviour 
{
    ...

    private float rotationX;

    ...

    void Rotate(float _camRot)
    {
        rotationX += _camRot * sensitivityY;
        rotationX = Mathf.Clamp(rotationX, -75, 50);
        camTrans.localEulerAngles = new Vector3(rotationX, camTrans.localEulerAngles.y, camTrans.localEulerAngles.z);

        rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation * sensitivityX));
    }
}

我希望它对你有帮助。


推荐阅读