I will write this once more
transform.rotation = Quaternion.Euler(...)
The rest of your message shows that you’re not entirely sure what quaternions are, but feel weirdly compelled to engage in a heated discussion.
Here, I’ll waste some time trying to explain what quaternions are, for the thousandth time.
First of all: when it comes to rotations in Unity nothing but quaternions exists. Yes you read that right, it all boils down to quaternions. So pay attention to what I’m writing in my posts, it’s not an opinion. There are NO Euler rotations, there are only conversions to Euler angles, to and from quaternions.
Why the whole world uses quaternions? Because they are superior to any other solution, both computationally and because they have almost zero drawbacks, such as gimbal lock as a classical example. Are they intuitive? Most people would immediately say ‘No’, but I would reply with ‘it depends’. Why would I say that, aren’t the values obviously nonsensical? Well, they are not nonsensical if you try to understand what they represent, and then work around them to figure out a new way to intuit about them.
Let me get this straight, are colors intuitive? What exactly is intuitive about them? Here I claim that they are not intuitive, they are completely crazy, they only become slightly more easier to grasp once you adopt the RGB model and separate them into their constituent colors. You see, every common sense regarding colors you’ll ever find is always something about the constituent base colors which you can mix together and so on, otherwise there is nothing intuitive about arriving from red to blue, and even this continuity changes its trajectory significantly with each and every color model.
So it’s the RGB color space that makes some sense for most people, and they also like Euler’s angles for a similar reason. You just add a little bit of rotation X with rotation Y and voila you get some compound rotation.
Now get this: a unit quaternion (a type of a quaternion which is used for rotations) also known as versor, which simply carries what is known as Euler vector to angle-axis representation (shown in post #4) which allows for a more robust computation (because it effortlessly translates into matrix computation), has a simple rule: it must be of norm 1. Long story short, it’s normalized similarly to how we normalize vectors. In this sense, it literally behaves like Vector4 would.
Vector3 Normalize(Vector3 v) {
var mag = MathF.Sqrt(Vector3.Dot(v, v));
return new Vector3(v.x / mag, v.y / mag, v.z / mag);
}
Quaternion Normalize(Quaternion q) {
var mag = MathF.Sqrt(Quaternion.Dot(v, v));
return new Quaternion(q.x / mag, q.y / mag, q.z / mag, q.w / mag);
}
However, unlike with vectors, zero quaternion is illegal, because (being versors) they must be unit. So the actual normalization does this instead.
Quaternion Normalize(Quaternion q) {
var mag = MathF.Sqrt(Quaternion.Dot(v, v));
if(mag < Mathf.Epsilon) return Quaternion.identity;
return new Quaternion(q.x / mag, q.y / mag, q.z / mag, q.w / mag);
}
What is this identity? Well for unit quaternions that’s the basic assumed nominal state of (0, 0, 0, 1). Why is this combination special? Because mathematical identities, by definition, when applied to other states, do nothing. Similarly, a value of 1 in real number multiplication behaves as an identity. Because multiplying by 1 does not change the resulting value.
If you observe what x, y, z do from the AngleAxis implementation above, it becomes apparent that they somehow correlate with the axes in 3D space, steering the axis component around the origin.
And with all this in mind now we can safely arrive to a newfound place from which we can develop a new intuition regarding quaternions. First of all, their components are always of such combined magnitude to never go below or above the length of 1. Second, that ‘w’ component seems to be special. As if the identity is trying to say “no power, no power, no power, full power”. Full power of doing what? Doing nothing of course.
I will make 4 cardinal quaternions by setting one component to 1 to help you observe what each value does.
Remember, the formulas go like this
x = axis.x * sin(angle/2)
y = axis.y * sin(angle/2)
z = axis.z * sin(angle/2)
w = cos(angle/2)
So we can reverse them if we know the final values and assume the axis to be of length 1.
For x,y,z it’s angle = 2 * arcsin(x,y,z)
for w it’s angle = 2 * arccos(w) but I will omit that because it literally serves as a lingering accumulator to satisfy the norm 1 requirement
(1, 0, 0, 0) => (180°, 0°, 0°) => rotate by 180° on X
(0, 1, 0, 0) => (0°, 180°, 0°) => rotate by 180° on Y
(0, 0, 1, 0) => (0°, 0°, 180°) => rotate by 180° on Z
(0, 0, 0, 1) => (0°, 0°, 0°) => do nothing
These values can be negative as well, so let’s check that too
(-1, 0, 0, 0) => (-180°, 0°, 0°) => rotate by -180° on X
(0, -1, 0, 0) => (0°, -180°, 0°) => rotate by -180° on Y
(0, 0, -1, 0) => (0°, 0°, -180°) => rotate by -180° on Z
(0, 0, 0, -1) => (0°, 0°, 0°) => do nothing
Now this doesn’t mean that you can naively do (0.5, 0, 0, 0) and get a rotation of 90° on X.
This is exactly why Unity docs say “do not attempt to tweak the quaternions directly”.
This also doesn’t mean that you can naively do (0.5, 0, 0, 0.5) and get a rotation of 90° on X.
No, this means that you can naively do (√2/2, 0, 0, √2/2) and actually get a rotation of 90° on X.
Why? Because we have to satisfy x² + y² + z² + w² = 1 at all times.
(√2/2)² + 0 + 0 + (√2/2)² = 2(√2/2)² = 2*(2/2²) = 4/4 = 1
√2/2 also happens to be sin(90°/2) so that’s an easy way to do this properly
You can do this to make this much easier (I don’t recommend it, this is for learning purposes)
var q = new Quaternion(0.5, 0f, 0f, 0.5).normalized
Try it:
Debug.Log(q);
Debug.Log(Quaternion.Euler(90f, 0f, 0f));
Now let’s return to colors. Can you appreciate this newfound intuition? We’ve literally taken half power from “nothing” and added it to the “X rotation” to obtain a half rotation on X. The values reflect this even though they’re not “intuitive” if you’re unfamiliar with the length of a vector in a four-dimensional space.
Again, there are no Euler’s angles in Unity, it’s all just a convenient translation for those who don’t or won’t understand quaternions. And yet, because this conversion math lacks any context, any naive conversion from a quaternion back to Euler’s angles will seem like “garbage” to you. That’s because there are no Euler’s angles to begin with, but the result will be mathematically correct, sadly without any guarantee of continuity or awareness of your specific use case.
To untangle this confusion in your mind, I strongly advise you to work with the quaternions, not against them, to learn them, understand them, and come up with incredibly robust and powerful ways of isolating your rotations to simple planes instead of fighting with the Euler angles, which is like trying to repair a wrist watch with a circular saw.