Search Unity

Problems with rotation of gameobject (Gimbal Lock)

Discussion in 'Scripting' started by AnupamSahu, May 19, 2015.

  1. AnupamSahu

    AnupamSahu

    Joined:
    May 16, 2015
    Posts:
    8
    Hello, i have written a script in unity to control an aircraft... it allows the airplane to roll to a limited amount of 90 degs, turn about the y axis , and have a pitch (to elevate).. everything works fine except when the plane reaches a rotation of 270 degs on x-axis... it starts rotating erratically and never does a 360 deg flip... Please help and thanks for reading.. Here's my script :

    Code (CSharp):
    1. void TurnRollPitch()
    2.     {
    3.         float yawTorque;
    4.         yawTorque = Input.GetAxis("Horizontal")*yawEffect;
    5.  
    6.         euler = transform.rotation.eulerAngles;
    7.         euler.z = Mathf.Lerp(euler.z , -Input.GetAxis("Horizontal")*rollEffect , 1.0f);
    8.         Quaternion addRot = Quaternion.Euler(euler);
    9.         transform.rotation = Quaternion.Slerp( transform.rotation , addRot , sensitivity );
    10.  
    11.         transform.Rotate( Input.GetAxis("Vertical")*pitchEffect , 0f , 0f );
    12.         Airplane.AddTorque( yawTorque*Vector3.up );
    13.         Airplane.angularDrag = (yawTorque==0)?10.0f:3.0f; //Prevents rotaion due to inertia.
    14.     }
    15.  
     
  2. Paul Bernhardt

    Paul Bernhardt

    Joined:
    Oct 9, 2013
    Posts:
    5
    I would avoid doing math with Euler angles, stick with Quaternions as much as possible or you're going to end up with weird issues around the extremes like that.
     
  3. AnupamSahu

    AnupamSahu

    Joined:
    May 16, 2015
    Posts:
    8
    thanx but how do i fix that now.. :rolleyes: