- Home /
Rotation - Why doesn't this work?
When I'm working with object rotations via script, it always seems to be a hit or miss situation.
All I'm trying to do, is have my script to change the rotation of my object on the X axis by ONLY 10
leftTail.transform.rotation = Vector3(10,0,0);
It just seems to get unnecessarily confusing with Unity involving Quaternions.
Answer by aldonaletto · Mar 29, 2013 at 11:53 PM
That's not a Quaternion's fault: rotations are always confusing in a 3D world. A quaternion actually is a rotation of some angle about an arbitrary axis - it's thus a simple angle-axis rotation, coded in a convenient way (convenient for mathematicians, of course).
In this case, if you want to simply rotate 10 degrees about the local X axis, use Rotate:
leftTail.transform.Rotate(10,0,0);
If you want to rotate about the global X axis, specify Space.World:
leftTail.transform.Rotate(10,0,0, Space.World);
If you want to rotate to the angle (10,0,0), use eulerAngles:
leftTail.transform.eulerAngles = Vector3(10,0,0);
If the leftTail object must be rotated relative to its parent, use localEulerAngles instead:
leftTail.transform.localEulerAngles = Vector3(10,0,0);
NOTE: eulerAngles is actually transform.rotation converted to Euler angles. You can precisely set eulerAngles to any 3-axes rotation you want, but when reading eulerAngles you may get weird and unexpected XYZ combinations at certain quadrants. Due to this, doing things like reading the current euler angles, modifying and writing it back may produce weird results.
@aldonaletto Tried all this and it still doesn't work. :( What may be the problem right now ?!
What means "doesn't work"? Does it rotate to some unexpected angle or it simply doesn't rotate at all?
Answer by olas72 · Mar 29, 2013 at 11:36 PM
try
leftTail.transform.localRotation = Quaternion.Euler(Vector3(10,0,0));
or
leftTail.transform.rotation = Quaternion.Euler(Vector3(10,0,0));
if you want the turn to be in world space
Answer by robertbu · Mar 29, 2013 at 11:49 PM
You can use Transform.eulerAngles.
leftTail.transform.eulerAngles = Vector3(10,0,0);
Quaternions use a 4x4 matrix and don't directly map to angles as you think of them. Don't manipulate the individual components of a Quaternion (x,y,z,w) directly unless you understand Quaternions. Note eulerAngles can also be tricky. You want to set all three (x,y,z) at the same time (as you do here with your Vector3). In addition, you cannot expect any specific values from eulerAngles. Any given "physical" rotation has multiple euler angle representations. For example you might set eulerAngles to (180, 0, 0), only to see them change to (0,180,180) instead.
Your answer
Follow this Question
Related Questions
Incrementing Rotation Produces Strange Transform Values 1 Answer
Cannot instantiate rotation on Prefab 1 Answer
Determining Look Rotation As > A Number? 1 Answer
Rotate camera around player 2 Answers
change the rotation 1 Answer