- Home /

# Avoiding Gimbal Lock

Hey,

I am stuck trying to rotate an object around the X axis. I have tried numerous approaches but I have not been able to rotate the object past 90 and 270 angle. I have converted all my code to quaternions but whatever I do, I cannot get it to work.

This is my current code, I would appreciate it if someone could point out where I am going wrong. Thanks

```
void Update ()
{
//get inputs
pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed);
yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed);
roll = Input.GetAxis("Roll") * .5f;
//implement throttle
thrust += Input.GetAxis("Throtle") * (Time.deltaTime * throtleSpeed);
thrust = Mathf.Clamp(thrust, 10000, 60000);
//smoothen rotaions
smoothPitch = Mathf.LerpAngle(smoothPitch, pitch, Time.deltaTime * pitchSmoothness);
smoothYaw = Mathf.LerpAngle(smoothYaw, yaw, Time.deltaTime * yawSmoothness);
smoothRoll = Mathf.LerpAngle(smoothRoll, roll , Time.deltaTime * yTurnSmoothness);
//gameobject rotation
myTransform.rotation *= Quaternion.Euler(-smoothPitch, smoothRoll + smoothYaw, 0);
//Have also tried this-
//myTransform.Rotate(-smoothPitch, smoothRoll + smoothYaw, 0);
//add thrust
myRigidbody.velocity = myTransform.forward * (Time.deltaTime * thrust);
}
```

*suggestion removed*

Check this link out : http://keithmaggio.wordpress.com/2011/07/01/unity-3d-code-snippet-flight-script/

That won't help - you're still converting it to the Euler representation, which is what causes Gimbal Lock.

**Answer** by Hoeloe
·
Nov 12, 2013 at 11:43 PM

I think the issue is that you're directly modifying the Euler angles - removing any benefit you get from having them internally stored as Quaternions. When you get the eulerAngles vector, or use Quaternion.Euler, you are essentially taking the representation in 3-dimensions and converting it to a quaternion. Much the same as if you convert a Vector2 to a Vector3, you don't gain any information by doing this, and so you lose the benefits of the Quaternion.

Instead of modifying the Quaternion via the Euler angles, try using the Quaternion.RotateAround method. This allows you to rotate the Quaternion around a specified axis, without every converting it to the Euler representation (and thus avoid Gimbal Lock). If you call this 3 times, one for each spacial axis, you should get the effect you want.

Quaternion.RotateAround does not exist. I'm wondering if you meant Quaternion.AngleAxis or something else?

Sorry, you're right - it's `Transform.RotateAround`

, you don't even need to bother going into Quaternions, though you can also do it with `Quaternion.AngleAxis`

on each axis and multiplying them together (quaternion multiplication applies each rotation in turn).

**Answer** by zombience
·
Nov 13, 2013 at 12:25 AM

try Quaternion.AngleAxis

```
public int xRot = 0;
void Update ()
{
transform.rotation = Quaternion.AngleAxis(xRot, transform.right);
xRot++;
xRot = (int)Mathf.Repeat(xRot, 360);
}
```

### 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.

### Follow this Question

### Related Questions

Difference in rotation without gimbal lock 0 Answers

Reverse rotations problems... 2 Answers

Yaw Pitch Roll order? 0 Answers