- Home /
Object's rotation is messed up whenever it's x reaches 90
I am very new to Unity, and I am making a game where I need to be able to fly an airplane. I've been implementing the roll and yaw of the aircraft, and everything works, except when the x of the rotation reaches 90 - then the object is instantly rotated in all axes in a very strange way, and I can't tell what part of my code does this. If you have the time to help, it would probably be good to apply the following code to a cube or something to see what I mean. Many thanks for any help!
public class PlayerMovement : MonoBehaviour
{
// Variables controlling roll of the aircraft;
private Vector3 rollRotation;
[SerializeField] private float rollSpeed = 100;
// Variables controlling yaw of the aircraft;
private Vector3 yawRotation;
[SerializeField] private float yawSpeed = 25;
private void FixedUpdate()
{
PlayerRoll();
PlayerYaw();
}
private void PlayerYaw()
{
if (Input.GetKey(KeyCode.A))
{
yawRotation = Vector3.down;
}
else if (Input.GetKey(KeyCode.D))
{
yawRotation = Vector3.up;
}
else
{
yawRotation = Vector3.zero;
}
Vector3 finalYawRotation = yawRotation * yawSpeed * Time.deltaTime;
Vector3 rot = transform.rotation.eulerAngles + finalYawRotation;
rot.y = ClampAngle(rot.y,-20f,20f);
transform.localRotation = Quaternion.Euler(rot);
float ClampAngle(float angle, float from, float to)
{
if (angle < 0f) angle = 360 + angle;
if (angle > 180f) return Mathf.Max(angle, 360 + from);
return Mathf.Min(angle, to);
}
}
private void PlayerRoll()
{
// Checks which direction to apply roll, if any, and then applies it;
if (Input.GetKey(KeyCode.Q))
{
rollRotation = Vector3.left;
}
else if (Input.GetKey(KeyCode.E))
{
rollRotation = Vector3.right;
}
else
{
rollRotation = Vector3.zero;
}
Vector3 finalRollRotation = rollRotation * rollSpeed * Time.deltaTime;
Vector3 rot = transform.rotation.eulerAngles + finalRollRotation;
transform.localRotation = Quaternion.Euler(rot);
}
}
Answer by Captain_Pineapple · Mar 17 at 10:26 AM
The issue you encounter here is a really really really common one for which i don't understand why Unity does not emphazise this issue in their tutorials.
Short version: Any rotation always has multiple representations as euler angles. (not counting that (180,0,0) would be the same as (180+360,0,0))
The only way to ensure consistent behaviour is by using quaternions as a quaternion is always unique for the representation of a rotation.
As unity always calculates rotations using quaternions and all euler angles are just used to make things readable for us humans, the result can thus jump from one representation to another when certain angle thresholds are reached.
That being said: there are hundreds of questions on this platform which solve the issue for clamped rotations. Please try to find a solution for this in there. If you are stuck with something specific, feel free to return and let us know.
Right, in addition any combination of euler angles (and there are generally 6 different ways you can arrange the 3 angles hierarchically), it's always the middle axis that will cause a gimbal lock problem whenever it hits +- 90°. It's an inherent property of the way euler angles work. See this video for an explanation. Euler angles always suffer from gimbal lock. Quaternions do not have this issue because any relative rotation and orientation can be represented by a single rotation around a distinct axis. That's the beauty of quaternions. You can reach any orientation from any other orientation by a single rotation around a single 3d axis. That's what a quaternion essentially represents. Unfortunately quaternions are not that intuitive (especially since its math involved 4d complex numbers) but in most cases you don't have to care about what the numbers mean in a unit quaternion. Though if you're curious I can recommend the numberphile video on quaternions (the "extra bits" at the end may also help to better understand the non-comutativity).
Your answer
Follow this Question
Related Questions
How can I rotate an object to be perpendicular to a surface it hits (in 2D); 1 Answer
How do I rotate a quaternion with euler angles? 0 Answers
Limiting rotation of object, specifically using scroll wheel 2 Answers
Changing object rotation in animation automatically changes it's position 1 Answer
How to make the character move and rotate where the camera is facing (3rd person) 0 Answers