Hello,
I’ve been looking this up for a while, but I can’t seem to find out why this is happening.
I’m using this line of code:
transform.rotation = Quaternion.Euler(transform.rotation.x, 180f, transform.rotation.z);
At runtime, this makes the object’s Y rotation negative 180. Conversely, if I try setting it to -180f, the object’s Y rotation will become positive 180.
I don’t understand Quaternions at all, but I would think that setting the object’s rotation would actually set it to the number I specified.
Basically, I was wondering if there was some kind of math related reason this happens, or if I’m just doing something wrong.
I’m not looking for a solution, just a bit of information. Any help is greatly appreciated, thank you.
The problem is that the Euler angles you’re setting are just a convenient human-readable representation. You’re creating a Quaternion from that set of euler angles, and that quaternion could just as easily been created with a value of -180, 180, -540, -900, 540, 900, etc… All of those are equivalent, and any of them being used for the y rotation will create the same quaternion. When you convert a quaternion to euler angles later, for example when the euler angles are printed in Unity’s inspector, or you read transform.eulerAngles
it picks some set of euler angles that is equivalent to the Quaternion. It has no memory of which one you used originally, but it will spit out one of those equivalent sets of angles. Euler angles are not unique, there are an infinite number of possible sets of euler angles that will result in the same rotation.
1 Like
That makes a lot of sense, thank you!
I see now why some of the stuff I was doing wasn’t working like I expected. transform.rotation
giving me Quaternions and not Euler Angles was really throwing me for a loop before haha.
Much appreciated!
1 Like