HELP! Hey Unity forum, need help dealing with Gimbal Lock

I’m using eulerAngles to rotate a platform for my tilt maze game I keep experiencing gimbal lock and I have no idea how to get around it. Here is my code, help would be much appreciated!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tilt : MonoBehaviour {

public Vector3 currentRot;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

currentRot = GetComponent().eulerAngles;

if ((Input.GetAxis (“Horizontal”) >.2) && (currentRot.z <=10 || currentRot.z >=348))
{
transform.Rotate(0, 0, 1);
}

if ((Input.GetAxis (“Horizontal”) <-.2) && (currentRot.z >= 349 || currentRot.z <=11))
{
transform.Rotate(0, 0, -1);
}

if ((Input.GetAxis(“Vertical”) > .2) && (currentRot.x <= 10 || currentRot.x >= 348))
{
transform.Rotate(1, 0, 0);
}

if ((Input.GetAxis(“Vertical”) < -.2) && (currentRot.x >= 349 || currentRot.x <= 11))
{
transform.Rotate(-1, 0, 0);
}
}
}

Pls use codetags. Nobody reads unformatted code.
The simple answer is don’t use euler angles to avoid gimbal lock.
You’ll probably want to use this: Unity - Scripting API: Quaternion.Lerp
And to check how far you rotated you check the angle with this: Unity - Scripting API: Quaternion.Angle

1 Like