How to Set a Single Axis Rotation of a GameObject?
What is the most efficient way of setting a rotation of a single axis of a GameObject without changing the other values?
Answer by Ashkan_gc · Dec 28, 2009 at 06:08 PM
what jaap did can be done by transform.rotate and i think it's not what you want and you want to set the value directly. you can use his code but remove the "rot*" and then the code will be
transform.rotation = Quaternion.Euler(0, 90, 0);
if you want to add some value to any of the x,y,z values simply use
transform.Rotate (0,0,10);
this will add 10 to Z value
This answer is clearly incorrect.. it sets all axes of rotation.
Answer by PatHightree · Dec 14, 2009 at 03:49 PM
I think this is what you're looking for :
// Sets the transforms rotation to rotate 30 degrees around the y-axis
transform.rotation = Quaternion.AngleAxis(30, Vector3.up);
I tired it and It resets other angles! That's not what he wants.
It has to be multiplied by the original rotation if you do it this way.
Quaternion originalRot = transform.rotation;
transform.rotation = originalRot * Quaternion.AngleAxis(degrees, Vector3.Up);
Answer by Vishvesh8 · Oct 17, 2018 at 03:49 AM
Try this transform.rotation = Quaternion.Euler(transform.eulerAngle.x, 30, transform.eulerAngle.z);
This is exactly what I needed and what whoever asked the question needed as well. THANK YOU
Answer by Jaap Kreijkamp · Dec 14, 2009 at 06:09 AM
Don't know what you exactly want but maybe this is it:
var rot = transform.rotation;
transform.rotation = rot * Quaternion.Euler(0, 90, 0); // this is 90 degrees around y axis
doesn't this set x and z to zero? what is you want to leave them unchanged?
Answer by Jessy · Dec 14, 2009 at 05:45 AM
The rotations are stored as quaternions, so you can store the appropriate quaternion and set transform.rotation to that.
Perhaps an explanation of what you are trying to accomplish would help. If you go into "Debug mode" and watch the rotation values change, you can see how just changing one axis's value isn't really more efficient. It only looks that way in "Normal mode".