R
R
RandomProgrammer2021-04-10 17:23:33
Unity
RandomProgrammer, 2021-04-10 17:23:33

Camera rotates incorrectly in Unity?

I wrote the following script to rotate the camera by mouse movement:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript0 : MonoBehaviour
{
    private Vector3 LastMousePosition;
    private Vector3 NowMousePosition;
    // Start is called before the first frame update
    void Start()
    {
        LastMousePosition = Input.mousePosition;
    }

    // Update is called once per frame
    void Update()
    {
        NowMousePosition = Input.mousePosition;
        transform.Rotate(0, NowMousePosition.x - LastMousePosition.x, 0);
        transform.Rotate(LastMousePosition.y - NowMousePosition.y, 0 ,0);
        LastMousePosition = NowMousePosition;
    }
}

When I move the mouse strictly vertically or strictly horizontally, everything is fine, but when I move vertically and horizontally at the same time, the camera turns to the side.
The script itself is attached to the camera.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question