当我点击播放时相机改变位置

Camera Changing Position When I Hit Play

因此,我正在尝试实现 FPS 模型,并且相机位于该模型的视线水平线上。

模型基于层次结构中的多个对象(我已经从子对象中删除了所有碰撞器) 模型有人物控制

相机是该模型的子项。

我已经正确排列了所有内容。 如果我手动旋转模型,相机会相应旋转,如果没问题的话。

但是当我在模拟开始时点击播放按钮时,相机开始旋转 (0,0,0)

我不明白发生了什么。我尝试在模型旋转的开始方法中手动面对相机。但这不起作用。

播放前 https://imgur.com/a/FTWmNWt

播放后 https://imgur.com/a/kXfFEHy

鼠标外观脚本

public class mouselook : MonoBehaviour
{
    public float mousesens = 100f;
    public Transform playerBody;
    float xRotation = 0f;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        // transform.rotation = playerBody.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mousesens * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mousesens * Time.deltaTime;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        // transform.Rotate(Vector3.up * mouseX);

        playerBody.Rotate(Vector3.up * mouseX);




    }
}

移动脚本(IDK,如果与此问题相关)

public class movement : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 12f;
    public float gravity = -9.81f;
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public float jumpHeight = 3f;

    public LayerMask groundMask;
    Vector3 velocity;
    bool isGrounded;
    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {

            velocity.y = -2f;
        }
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y += Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}

我怀疑,这将是一个简单的答案,我已经浪费了几个小时。

问题在于这一行,在鼠标脚本中

 transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

必须将 x 轴设置为不同的值。它完美地工作,在运动脚本中做了一些改变,改变了一些方向。

鼠标脚本

transform.localRotation = Quaternion.Euler(xRotation, 70f, 0f);

运动脚本

Vector3 move = transform.right * z + transform.forward * -x;

谢谢大家