Controlling pitch , yaw and roll of an airplane

So i have been trying to rotate an airplane 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…:face_with_spiral_eyes::frowning:
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.

    }
}

I have some turret code I can post when I get home if you need it though I’m not 100% thrilled with it. Love to see an out of the box solution for rotation constraints. I think no one has made a solid asset for this as only 3 humans in the world can do proper quaternion rotation. p.s not sure my code will work for you as it uses 2 gameobjects to simplify the math.

no problem i will give it a try… thank you… anyways who are the 3 humans :roll_eyes:

Hey you must try out the above code by adding it to a airplane gameobject and you will get to know the problem when the airplane rotates 90 degs on x axis