Player Rotation

Hey, I’m writing a script for player rotation, and it looks like this:

float axisY;
float axisX;
float axisZ;
void Update()
{
        axisX = Input.GetAxisRaw("Mouse X");
        axisY = Input.GetAxisRaw("Mouse Y");
gameObj.transform.Rotate(axisY, -axisX, 0);
}

and it works, but there are problem. When I’m moving mouse, player rotation on z axis is changing too, just small values, but still. How can I fix it?

You need to use quaternions, don’t ask me though i don’t know about them

So, I read something about quaternions, and my problem disappear… But new appear xD My changed code:

gameObj.transform.rotation = Quaternion.Euler(gameObj.transform.rotation.x-Input.GetAxis("Mouse Y"), gameObj.transform.rotation.y-Input.GetAxis("Mouse X"), 0);

Every time player change his rotation, it back to 0. I suspect, that I just don’t understand entire quaternions, because english is not my native language, and I don’t understand the descriptions of methods. What is wrong with this code? Maybe should I use another method?

Jaja yeah my problem exactly.
I think this should work.

//For more direct understunding when using input with rotations.
//Since x=>y and y=>x, plus sensitivity implementation
input.x = Input.GetAxis("Mouse Y")*sensitivityX;
input.y = Input.GetAxis("Mouse X")*sensitivityY;

targetRotationX += input.x;
targetRotationY += input.y;

Quaternion xRot = Quaternion.Euler(Vector3.right *targetRotationX );
Quaternion yRot = Quaternion.Euler(Vector3.right *targetRotationY );

transform.rotation = yRot * (xRot * Quaternion.Identity);
//edit: rather try this line instead
transform.rotation = xRot* (yRot * Quaternion.Identity);
//First rotate on Y and then look up/down, that's the idea, else I think it will end up looking the wrong way.

The idea is to store the rotations as floats and then turn them into a quaternion, I don’t understand the math but I think its something like this.

If this question isn’t answer yet with the response, can you describe which rotations you’re looking for and whether this is 2d or 3d and if there are any rotation limits, etc… :slight_smile:

anglex += Input.GetAxis("Mouse X");
        angley +=  Input.GetAxis("Mouse Y");
        Quaternion xRot = Quaternion.Euler(Vector3.up * anglex);
        Quaternion yRot = Quaternion.Euler(Vector3.right * angley);
        gameObj.transform.rotation = xRot * (yRot * Quaternion.identity);

Only this one “right” was uncorrect, I change it and it works, thanks a lot :smile:
Edit: Onle last request: can you explain how it works?

1 Like

Sure, starting from the end what you want to do is start with the default rotation 0º,0º,0º (Quaternion.Identity) and rotate it.
For what I have gathered, multiplying a quaternion by another quaternion rotates it that amount, but the target quaternion has to be in the right side of the multiplication and the rotation on the left side. So you first rotate it in Y (yRot*Identity) and you get the rotation 0,rotY,0. And then you rotate it in X xRot * (result).

The idea of storing the rotations as floats is to keep it simple, and since the euler to quaternion of a rotation in just one angle is (at least i think) a solid conversion with no danger, you should work with them separately and it should give you the bests results. Usually transforming an euler like (0,50,190) or (60,5,250) is not going to have an expected transformation into a quaternion, since one quaternion have multiple euler interpretations.