I’m really stumped by this one and hoping someone else has seen this and has an idea of what might be going on…
The long and the short of it is I have a script that is calling Quaternion.Euler
for example:
Debug.Log($"projectile direction: {direction} - {Quaternion.Euler(direction)} - {transform.position}");
but the results are unexpectedly ALWAYS (0,0,0,1)
notice, that no matter what VECTOR3 (direction) gets passed in the derived Euler is the same.
This doesn’t make any sense to me. Can anyone else explain? For reference, this is in Unity 20108.4.12f1
Two things here.
First is precision - Vector3 and Quaternion only show you up to 1 decimal point, so while these appear equal, they’re not.
Second is, you don’t seem to be thinking of “euler angles” correctly. Euler angles are in degrees - you rotate X degrees around the right-pointing axis, Y degrees around the up-pointing axis, and Z degrees around the forward-pointing axis. Your numbers are all less than 1, so very small rotation values there, easily enough to get lost in a rounding error.
Those look like direction vectors, in which case you should be using Quaternion.LookRotation, not Euler.
For some further reading, here’s an article about Euler angles. It’s not directly relevant in this case, but at some point in the future I guarantee you’ll run across issues with Euler angles (they’re terrible).
3 Likes
The value of “direction” in each output line is almost identical. Try using a much bigger difference in the angle, or output the individual members of the quaternion because quaternion.ToString rounds to the nearest tenth.
1 Like
Since they called it “direction” I assume they want a rotation that points in that direction.
Try something like Quaternion.LookRotation if that’s what you want:
Cause yeah, as has been pointed out… Quaternion.Euler takes in per axis rotations in degrees around the x,y,z axes. Not a “direction” vector. It accepting Vector3 is incidental to the data type fitting 3 floats, it’s not actually treating the Vector3 as a mathematical ‘vector’ that represents mangitude and direction.
2 Likes
Thanks! Switching from Euler to LookRotation has indeed had the result I was hoping for. (the object is instantiating in the direction of my raycast and not a static direction).
The funny thing is I was taking this from a course where someone else’s sample code WAS using Euler and it WAS functioning as expected. I don’t know what was going wrong, but at the very least I’m unblocked on this one.