- Home /
Problems caused by non-unique Euler angle solutions
My title might not be accurate, if I'm not correct in guessing what's causing the problem here. I'm trying to get the Euler angles of an object (it's a plane/glider in a 2.5D sidescroller game) in a script to figure out its orientation, and also to determine force magnitudes (using sin and cos of elevations etc). The problem is that any given rotation doesn't have a unique solution as three Euler angles, and Unity doesn't always give the angles the way I want/expect.
For example, the plane has initial rotations (0, 90, 0), so that it's facing to the right of the scene. If I tilt the plane up and down in the scene view, its x rotation changes, as I'd expect, seeing as it's local x (/red/right) axis is pointing towards the camera. However, as I tilt it up past fully vertical (x=-90) so that it's pointing up and to the left in the scene, all of a sudden the y rotation flips to -90 instead of 90, and the x rotation starts coming back towards zero, instead of going past 90. You can check this pretty easily in the editor yourself. This makes it hard to work with Euler angles in code to figure out what's going on.
So I guess my question is has anyone dealt with the fact that Euler angles don't always come out how you expect in the past and found a good way to work around it?
Also, I'm confused as to how the rotation I create in 2 steps (y=90, then x=-105) which I would expect to be (-105, 90, 0), resolves to what unity gives me, which is (-75, -90, 0). With the blank box that I'm using, these rotations appear equivalent, but they're not really. In the first case, the original down-facing side of the box faces up and to the right. In the second case, it's facing down and to the left. The more I think about this problem, the more confused I get... :-/
Answer by Eric5h5 · Nov 06, 2010 at 04:44 PM
The best way is to track rotations yourself, like the standard MouseLook script does, for example. If that's not possible, such as trying to read back rotations from an object that's subject to physics and not under direct script control, then that's a different matter and I don't really know a good solution.
Nah it's all scripted, not using physics control on the plane, so I think that might be a good solution. The other idea I came up with is just to infer the angles from the transform's 3 unit vectors, but it's not as neat. Cheers.