What is the most efficient way of setting a rotation of a single axis of a GameObject without changing the other values?
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);
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
this can be done easily by transform.rotate
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
this will add 10 to Z value
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".
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
To help users post good questions and use the site effectively we have posted a user guide. Please check it out.
Answers and Comments
1 Person is following this question.
Flip over an object (smooth transition)
Snapping an object to another
Rotate bones during mecanim animation with generic avatar
transform.Rotate is rotating all axes, why?
I am getting a problem with moving and rotating a camera!