Home > Software design >  MouseX and MouseY Swapped in Unity
MouseX and MouseY Swapped in Unity

Time:03-16

I have checked my variables for a few minutes tweaking and checking variables. Whenever I move my mouse horizontal, the GameObject Rotates up and down. Whenever I move it vertical, it rotates left to right. Here is the code:

    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

    xRotation -= mouseX;
    yRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    yRotation = Mathf.Clamp(-90f, yRotation, 90f);



    transform.localRotation = Quaternion.Euler(xRotation, yRotation, 0f);
    cameraObject.Rotate(Vector3.up * mouseY);
    cameraObject.Rotate(Vector3.right * mouseX);

It is probably some dumb mistake I do not understand. I am also new to unity so please be nice. Lastly, some other information is that the GameObject is actually the camera, if that information helps.

If you need even more information, tell me so I can fix it.

Thank you for your time.

CodePudding user response:

Your code is right, your assumption is wrong. Unity uses a left handed y-up coordinate system, which means that x is pointing to you, y is pointing up, z is pointing to the right (assuming, you are looking against x).

Now rotations work around the axis, so turning something around x means that you are "rolling" the object.

You can see it pretty well when looking at Unity's translation gizmos:

The red arrow (x) is pointing forward, while the axis of the handle is located in the plane that x is a normal vector to.

So what I'd suggest you do is just flip your assignments and call it a day:

xRotation -= mouseY;
yRotation -= mouseX;

Also, if you want to verify if your inputs yield the desired result, why don't you just log mouseX and mouseY and see if the result matches your expectations?

Debug.Log($"({mouseX}, {mouseY})");

CodePudding user response:

Max Play's answer was correct, though i realized the X axis was still inverted. So I changed it to

xRotation -= mouseY;
yRotation  = mouseX;

I appreciate everyone's time and again, thanks to Max Play.

  • Related