S
S
Stalder2016-01-10 08:54:21
Unity
Stalder, 2016-01-10 08:54:21

unity. How to implement camera rotation that allows for a sudden change in rotation?

You need to be able to rotate the camera with the mouse. Previously it was implemented like this:

void Update()
    {
        X = Input.GetAxis("Mouse X") * speeds * Time.deltaTime;
        Y = -Input.GetAxis("Mouse Y") * speeds * Time.deltaTime;
        transform.rotation = Quaternion.Euler(Y, X, 0);
    }

However, when it became necessary to change the rotation of the camera, it was found that it immediately returns to its original position.
Remade:
void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame 
    void Update()
    {
        X = Input.GetAxis("Mouse X") * speeds * Time.deltaTime;
        Y = -Input.GetAxis("Mouse Y") * speeds * Time.deltaTime;
        transform.rotation *= Quaternion.Euler(Y, X, 0);
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            Cursor.lockState = CursorLockMode.None;
        }
    }

But now a strange rotation around the Z axis has appeared, although 0 is explicitly indicated in the code.
Please help me, how to get rid of this rotation? Or is there even a better way to solve the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Andryushchenko, 2016-01-10
@Stalder

private float X, Y, Z;
    public int speeds;
  private float eulerX=0, eulerY=0;
    // Use this for initialization
    void Start () {
    Cursor.lockState=CursorLockMode.Locked;
  }
  
  // Update is called once per frame
  void Update () {
        X = Input.GetAxis("Mouse X") * speeds * Time.deltaTime;
    Y = -Input.GetAxis("Mouse Y") * speeds * Time.deltaTime;
    eulerX = (transform.rotation.eulerAngles.x + Y) % 360;
    eulerY = (transform.rotation.eulerAngles.y + X) % 360;
        transform.rotation = Quaternion.Euler(eulerX, eulerY, 0);
    if (Input.GetKeyUp (KeyCode.Escape)) {
      Cursor.lockState = CursorLockMode.None;
    }
    }

It worked for me, the problems are on your side.

X
xmoonlight, 2016-01-10
@xmoonlight

just change the sign "*=" to "="
rotation 721 degrees = 1 degree)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question