Hi, I’ve written a little script for controlling an object like a jetpack. I also wrote some script for righting the player so that they’re not constantly fighting to stay in control. However, whenever the object rotates past 180 degrees (even if they rotate like a top), the self righting code freaks out.
Example of rotational script:
Doesn’t do what (I’m guessing) you think it does. ‘rotation’ is a quaternion; although the value of rotation.x does have a meaning, I wouldn’t rely on it for something like this. (For one thing, q and -q represent the same rotation, so whether x is > 0 or < 0 in this particular case could be arbitrary.)
Maybe what you’re wanting is Transform.eulerAngles or Transform.localEulerAngles. However, I wouldn’t recommend using either of those for this either. If you want to use something like your current method, you could determine how the player is oriented by measuring the angle of the forward vector relative to the ground plane (or even just checking the y value). Another approach that often works well for this sort of thing is to apply a corrective rotation over time that rotates the local up vector onto the world up vector (which the method you’re using will do as well, more or less, but the ‘shortest arc’ rotation method is a bit more general).