Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Controlling pitch , yaw and roll of an airplane

Discussion in 'Scripting' started by AnupamSahu, Jun 3, 2015.

  1. AnupamSahu

    AnupamSahu

    Joined:
    May 16, 2015
    Posts:
    8
    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...:confused::(
    please suggest some alternative methods to get the desired results without gimbal lock.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestScript : MonoBehaviour {
    6.  
    7.     public float rollAngle = 90f;
    8.     public float pitchSpeed = 2f;
    9.     public float turnSpeed = 3f;
    10.     private Rigidbody rigidBody;
    11.  
    12.     void Start()
    13.     {
    14.         rigidBody = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         Vector3 euler = transform.eulerAngles;
    20.         float turnTorque = 0;
    21.  
    22.         euler.z = Mathf.Lerp(euler.z , -Input.GetAxis("Horizontal")*rollAngle , 1.0f);
    23.         Quaternion addRot = Quaternion.Euler(euler);
    24.  
    25.         transform.rotation = Quaternion.Lerp(transform.rotation , addRot , 0.2f);
    26.         transform.Rotate(new Vector3(Input.GetAxis("Vertical")*pitchSpeed,0,0));
    27.  
    28.         turnTorque = turnSpeed*Input.GetAxis("Horizontal");
    29.         rigidBody.AddTorque(Vector3.up*turnTorque);
    30.  
    31.         rigidBody.angularDrag = (turnTorque==0)?10:3;//Prevents rotation due to inertia.
    32.  
    33.     }
    34. }
     
  2. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    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.
     
  3. AnupamSahu

    AnupamSahu

    Joined:
    May 16, 2015
    Posts:
    8
    no problem i will give it a try.. thank you.. anyways who are the 3 humans :rolleyes:
     
  4. AnupamSahu

    AnupamSahu

    Joined:
    May 16, 2015
    Posts:
    8
    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