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.

3 Answers

3

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

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 ?!

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.

Hi Ben, just saw your comment. Unfortunately I'm not too familiar with Sharp serializer or any of the plugin stuff really :) In fact I abandoned Windows Phone since my last post. It just became too much work trying to get plugins to behave. I'll just build for W10 at some point.