# Avoiding Gimbal Lock with Vector3 EulerAngles and Applying Torque

I'm trying to make a controller which will let a user apply torque to a spaceship around all three axes. When user input ceases, a damping torque will be applied to slow the rotational speed down to zero.

However, I'm getting a gimbal lock problem where the behaviour of the damping force becomes unpredictable at any rotation where the y and/or z axis rotations shift by 180 degrees instantaneously. All the proposed solutions I've come across so far use a direct manipulation of transform.rotate, which I wish to avoid the use of in favor of RigidBody's addTorque.

My current understanding of the problem is that this is being caused by the fact that Unity uses quaternions instead of EulerAngles to track rotation, but I lack the knowledge of how to solve it.

My current code:

```
Rigidbody shipRB;
void Update () {
getInput();
}
Vector3 lastRotationPos;
void getInput() {
//rotations
if (Input.GetKey("q")) {
shipRB.AddTorque(transform.right);
} else if (Input.GetKey("e")) {
shipRB.AddTorque(-transform.right);
} else {
Vector3 currentRotation = transform.rotation.eulerAngles;
Vector3 rotationDelta = lastRotationPos - currentRotation;
float xAngle = Mathf.DeltaAngle(lastRotationPos.x, currentRotation.x);
float yAngle = Mathf.DeltaAngle(lastRotationPos.y, currentRotation.y);
float zAngle = Mathf.DeltaAngle(lastRotationPos.z, currentRotation.z);
if (transform.eulerAngles.y >= 90 && transform.eulerAngles.z >= 90) {
xAngle = -xAngle;
}
rotationDelta = new Vector3(-xAngle, -yAngle, -zAngle);
//limiting correction thrust
rotationDelta = new Vector3(limit(rotationDelta.x, 5), limit(rotationDelta.y, 5), limit(rotationDelta.z, 5));
//apply correction thrust
shipRB.AddTorque(rotationDelta);
}
if (Input.GetKey("i")) {
shipRB.AddTorque(transform.forward);
} else if (Input.GetKey("k")) {
shipRB.AddTorque(-transform.forward);
}
if (Input.GetKey("j")) {
shipRB.AddTorque(transform.up);
} else if (Input.GetKey("l")) {
shipRB.AddTorque(-transform.up);
}
lastRotationPos = transform.rotation.eulerAngles;
}
float limit(float num, float limit) {
if (num > limit) {
num = limit;
} else if (num < -limit) {
num = -limit;
}
return num;
}
```

Why not just increase Angular drag component of the rigidbody?

Or use apply torque as shipRB.AddTorque(-shipRB.angularVelocity) if you really want to do it yourself. You seem to want to make things hard for yourself.

Thanks, increasing angular drag works perfectly. I'm not very familiar with the Unity libraries yet, and also have a habit of inadvertently making things hard for myself.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.