D
D
Dred Wolf2021-04-01 21:32:17
Unity
Dred Wolf, 2021-04-01 21:32:17

Why does the camera move 90* on the x-axis when the scene starts?

Implementing a simple 1st person camera control system. When the scene starts, the camera abruptly drops 90 degrees in X. The same code works fine in the tutorial I copied it from:

using UnityEngine;

public class MouseLook : MonoBehaviour
{
//скорость вращения
    public float mouseSens = 100f;
// игрок
    public Transform playerBody;
// угол вращения по X
    float xRotation = 0f;
    void Start()
    {
// фиксирую курсор в центре
        Cursor.lockState = CursorLockMode.Locked;
    }

    void Update()
    {
// получаю ввод с мышки
        float mouseX = Input.GetAxis("Mouse X") * mouseSens * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSens * Time.deltaTime;

         xRotation -= mouseY;
       // проблема происходит из-за этой строчки кода которая должна ограничивать вращения по оси X
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        
        playerBody.Rotate(Vector3.up * mouseX);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
namee, 2021-04-02
@namee

The default value of xRotation needs to be changed. Perhaps in the example it was public and the value was assigned in the editor.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question