• Advertisement
Sign in to follow this  

[Unity game engine] Quaternion "free-look" script suffering gimbal lock

This topic is 2156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Given that I'm operating solidly in three dimensions, this shouldn't happen - Provided I coded it properly.

The code in question:

using UnityEngine;
using System.Collections;

public class MouseLookFree : MonoBehaviour {

// The current orientation.
Quaternion orientation ;

// Use this for initialization
void Start () {
// If we get a quaternion rotation from World Forward to Local Forward,
// we effectively just got the quaternion orientation.
orientation = Quaternion.FromToRotation(
Vector3.forward,
transform.localToWorldMatrix.MultiplyVector( Vector3.forward )
) ;
}

// Update is called once per frame
void Update () {
// Make sure the orientation is updated.
orientation = Quaternion.FromToRotation(
Vector3.forward,
transform.localToWorldMatrix.MultiplyVector( Vector3.forward )
) ;

// Get a horizontal and vertical quaternion rotation from an angle and axis.
var rotateHorizontal = Quaternion.AngleAxis( Input.GetAxis( "Mouse X" ), Vector3.up ) ;
var rotateVertical = Quaternion.AngleAxis( Input.GetAxis( "Mouse Y" ), Vector3.left ) ;

orientation = rotateHorizontal * orientation ;
orientation = rotateVertical * orientation ;

transform.localRotation = orientation ;
}
}

My other thought is that Unity uses Euler angles as its underlying representation, but I want to eliminate user error before I say that.

Share this post


Link to post
Share on other sites
Advertisement
You're falling for the common misconception that just using quaternions is enough to avoid gimbal lock. That's not 100% true - the cause of gimbal lock comes from concatenating multiple successive rotations, and it doesn't matter what representation is used for those rotations. The advantage of quaternions in this case is that they can allow you to express the full set of rotations in a single move, which you can't do with Euler angles, but unless you actually do this you're still prone to gimbal lock.

So in your case you're using quaternions, but you're still concatenating 3 rotations. What you need to do instead is construct a single quaternion from your raw rotation data and use that instead.

Share this post


Link to post
Share on other sites

You're falling for the common misconception that just using quaternions is enough to avoid gimbal lock. That's not 100% true - the cause of gimbal lock comes from concatenating multiple successive rotations, and it doesn't matter what representation is used for those rotations. The advantage of quaternions in this case is that they can allow you to express the full set of rotations in a single move, which you can't do with Euler angles, but unless you actually do this you're still prone to gimbal lock.

So in your case you're using quaternions, but you're still concatenating 3 rotations. What you need to do instead is construct a single quaternion from your raw rotation data and use that instead.

...Huh, thanks. Working...

Share this post


Link to post
Share on other sites
Look what you can find on Google. :)


using UnityEngine;
using System.Collections;

public class MouseLookFree : MonoBehaviour {

public string Yaw = "Mouse Y" ;
public string Pitch = "Mouse X" ;
public string Roll = "Mouse ScrollWheel" ;


// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
transform.Rotate( -Input.GetAxis( Yaw ), Input.GetAxis( Pitch ), Input.GetAxis( Roll ) * 90, Space.Self ) ;
}
}

Next: Torque?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement