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 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 user guide.
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).
Answers and Comments
8 People are following this question.
Rotate an object to look at another object on one axis?
how would I make a capsule rotate and go down a platform on the y axis?
Joystick Angle Problem
Drag and rotate object similarly to Monument Valley rotation mechanics?
Rotating objects around other without rotating itself