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);
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 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 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 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".
Answer by ProgramacionWH
Sep 07, 2016 at 12:29 AM
You can try: transform.eulerAngle.x/y/z = NUMBER;
Firstly, do not comment an answer on a dead thread. The OP has most likely found their answer. Second, that doesn't work. transform.eulerAngles.(var) is read only, and in order for each one to be changed it requires a new Vector3 to do so.
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.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
8 People are following this question.
Why does rotating via viewport give different values than inspector rotations?
Unity - Problems with Bone Rotations within constraints
Flipping cards to face camera at weird angles
Weird rotation when using Slerp!!?
how to set a rotation without it constantly rotating?