Quaternion Rotations

Hi guys,

I’ve run into a bit of a problem with rotating an object in a certain way.

I’m trying to simply rotate an object around the x axis (rotating downwards and upwards) and around the global y axis (left and right) through user input.

The rotation around the local x axis works fine using:

transform.Rotate(transform.right * Time.deltaTime * rotate_speed * InputY);

I then used the following for the y axis rotation:

transform.Rotate(Vector3.up * Time.deltaTime * rotate_speed * InputX, Space.World);

Notice I used world space for the y rotation. (The user input is either -1 or 1)

This all works fine - I can press the up and down keys to make the object rotate on the x axis and the left and right keys to make the object rotate on the y axis.

However, when i rotate the object on both the x and y axis, the z axis also starts to change which makes the object rotate sideways.

This is really irritating, have been trying to figure out a solution the past 2 hours :wink:
Sadly I don’t really know much about using quaternions… So, is there a way to either reset the z rotation each frame or a better way of only rotating along a specific axis?


Well you could keep track of your desired rotation on X and Y in separate variables then:

   transform.rotation = Quaternion.Euler(xRotation,yRotation,0);