Quaternion.Euler doesn't work right?
This seems wrong:
Quaternion DesiredRot = Quaternion.Euler(90f, 0f, 0f);
Debug.Log(DesiredRot.eulerAngles.x); // DISPLAYS 90
Quaternion DesiredRot = Quaternion.Euler(100f, 0f, 0f);
Debug.Log(DesiredRot.eulerAngles.x); // DISPLAYS 80. Why?
Quaternion DesiredRot = Quaternion.Euler(110f, 0f, 0f);
Debug.Log(DesiredRot.eulerAngles.x); // DISPLAYS 70. Why?
Quaternion DesiredRot = Quaternion.Euler(120f, 0f, 0f);
Debug.Log(DesiredRot.eulerAngles.x); // DISPLAYS 60. WHY...?
And the documentation says the params should come in a different order, yet that's how it works.
Answer by Glabrezu · Jan 27, 2016 at 11:15 PM
Found an article about Quaternions.
If anyone else has this problem, you can't just assign a rotation to a Quaternion apparently. You take your initial Quaternion and then multiply it by the second one, which should contain the rotation which you want to apply to the first one.
So you should basically do it like that:
Quaternion initialRot = objToRotate.transform.rotation;
objToRotate.transform.rotation = initialRot * Quaternion.Euler(90f, 0, 0);
This example just rotates the object by 90 degrees.
Answer by DiegoWw · Jul 12, 2019 at 02:50 PM
@Glabrezu is correct. Additionally, you can "kinda" assign an Euler angle by doing:
objToRotate.transform.rotation = Quaternion.Identity * Quaternion.Euler(90f, 0, 0);
It's an old post but maybe will somebody anwser :)
Im working with Rotations in unity, had the same problem and found this discussion.
Sorry, but for me does the correct answer no sense.
$$anonymous$$y thoughts: Fakt is that:
Quaternion.Identity * Quaterion.Euler(45f, 45f, 45f) == Quaterion.Euler(45f, 45f, 45f)
Because of the property of Identity
I. e. my Initial Rotation is the Identity. Consequently:
Quaternion DesiredRot = initialRot * Quaternion.Euler(100f, 0f, 0f);
Is equal to Quaternion.Euler(100f, 0f, 0f).
$$anonymous$$y Question is: Why shows this
80° ins$$anonymous$$d of 100°? I did some tests with Quaternion.Euler(100f, 0f, -45f); converted Euler(100, 0, -45) to Quaternion, back to Euler and i will get (80.0, 180.0, 135.0). That's equal to an euler rotation of (-100, 0, -45) and that isn't right. Quaternion qTest= Quaternion.Euler(100f, 0f, -45f); Debug.Log(qTest.eulerAngles); I think there is a bug or I'm wrong? :)Debug.Log(DesiredRot.eulerAngles.x);
@zukahara, if you:
Debug.Log( Quaternion.Euler( 100f, 0, 0 ).eulerAngles );
You see printed: (80.0, 180.0, 180.0)
Those are two different representations of
exactly the same set of rotations
Remember that the order of Rotations matters. So first rotating x by 100, and then y by zero, and finally z by zero, ends up the same thing rotating x by 80, and then y by 180 and then z by 180.
To say that the Quaternion calculation is wrong because, "it took my 100 degrees and now it's 80 degrees" is a mistake in your thinking about it. You have forgotten to apply the rotations on the the other two axes.:
For any given set of rotations, there may be more than one set of equivalent rotations.
Debug.Log( Quaternion.Euler( 100f, 0f, -45f ).eulerAngles );
Yields: (80.0, 180.0, 135.0)
And Debug.Log( Quaternion.Euler( 80.0f, 180.0f, 135.0f ).eulerAngles );
Yields: (80.0, 180.0, 135.0)
Go figure! Once in Quaternion form, the representation has been optimized.
:
The important thing in Unity is to trust Quaternions and deal with Euler angles as little as possible. Do not convert from Eulers to Quaterinons and back to Eulers and Back to Quaternions in order to clamp rotations, or other operations. You can drive yourself crazy!:
Quaternions were invented by William Hamilton nearly two hundred years ago based on the mathematics of Leonhard Euler from a hundred years earlier. The are mathematical constructs that have been proven. They are perfect, as such constructs are.:
There are no bugs in Unity's Quaternion class!
The little correction to @streeetwalker
Remember that the order of Rotations matters. So first rotating x by 100, and then y by zero, and finally z by zero, ends up the same thing rotating x by 80, and then y by 180 and then z by 180
But it nothing change, He is absolute right
Transform.localEulerAngles ...
In Unity these rotations are performed around the Z axis, the X axis, and the Y axis, in that order.
Your answer
Follow this Question
Related Questions
Quaternion.LookRotation on only one axis? 1 Answer
Rotation problem 0 Answers
What's wrong with this Quaternion rotation? 1 Answer
Draw a ray from a gamobject and keep direction of the ray relative to the gameobjects rotation. 1 Answer
Rotate clockwise/counterclockwise using Quaternion.Slerp() 0 Answers