So i have been trying to rotate an airplane in UNITY along the x y z axes such that it has a free rotation in the x(pitch) and y(yaw/turn) axes but a limited lean/roll in the z axis.. I have written the following script , but it suffers from gimbal lock... pleaseee help...
please suggest some alternative methods to get the desired results without gimbal lock.
using UnityEngine;
using System.Collections;
public class TestScript : MonoBehaviour {
public float rollAngle = 90f;
public float pitchSpeed = 2f;
public float turnSpeed = 3f;
private Rigidbody rigidBody;
void Start()
{
rigidBody = GetComponent<Rigidbody>();
}
void Update()
{
Vector3 euler = transform.eulerAngles;
float turnTorque = 0;
euler.z = Mathf.Lerp(euler.z , -Input.GetAxis("Horizontal")*rollAngle , 1.0f);
Quaternion addRot = Quaternion.Euler(euler);
transform.rotation = Quaternion.Lerp(transform.rotation , addRot , 0.2f);
transform.Rotate(new Vector3(Input.GetAxis("Vertical")*pitchSpeed,0,0));
turnTorque = turnSpeed*Input.GetAxis("Horizontal");
rigidBody.AddTorque(Vector3.up*turnTorque);
rigidBody.angularDrag = (turnTorque==0)?10:3;//Prevents rotation due to inertia.
}
}