- Home /
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 ;)
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?
Regards
Answer by whydoidoit · Apr 22, 2013 at 03:14 PM
Well you could keep track of your desired rotation on X and Y in separate variables then:
transform.rotation = Quaternion.Euler(xRotation,yRotation,0);
Thanks for the reply, this seems like a great solution. Interestingly though the z axis will still rotate when using this code:
var xRotation = transform.rotation.eulerAngles.x + (Time.deltaTime * rotate_speed * InputX);
var yRotation = transform.rotation.eulerAngles.y + (Time.deltaTime * rotate_speed * InputY);
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
Do you have any idea why this could be?
You have to not use transform.rotation.eulerAngles as your basic item (because gimbal lock etc can cause it to change).
$$anonymous$$eep an xRotation variable as a float etc.
O$$anonymous$$ so I now have:
xRotation += Time.deltaTime * rotate_speed * InputX;
yRotation += Time.deltaTime * rotate_speed * InputY;
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
This seems to have fixed the problem of the object rotating sideways, but for some reason the z value is still changing between around 0 and 10.
Can't quite get my head around the reason why it is doing this, but overall the rotation now works quite nicely (Still getting bit of gimbal lock though).
Thanks for your help, my brain almost died.
Still the small question of why the z axis is not remaining at 0, but it seems like it doesn't interfere with the rotation like it did before.
Well the other thing you could try is doing this:
transform.rotation = Quaternion.AngleAxis(xRotation, Vector3.right) * Quaternion.AngleAxis(yRotation, Vector3.up);
Your answer
![](https://koobas.hobune.stream/wayback/20220613100824im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Freeze specific rotation axis of a child 2 Answers
Get rotation around (forward) axis ? 1 Answer
Object is rotating "step by step" 1 Answer
FBX model icon issues 0 Answers
Rolling a capsule lengthways, sideways and twisting with AddTorque 1 Answer