Search Unity

how to add torque to correct bank?

Discussion in 'Editor & General Support' started by tswalk, Oct 20, 2013.

  1. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    I have a simple script that applies pitch (Z axis) and yaw (Y axis) to an object using relative torque. However, I have noticed that the object will generate a natural roll on combined inputs and I am trying to figure out how to correct this while preserving the new heading. (does that even make sense?, I'm fairly new to using physics)

    here is an image:

    $banking.jpg


    the top image shows the original "world" (which is also starting local) vector (forward is aligned to X axis). The bottom image shows the rolling angle (red dotted line) that the arrow needs to be corrected. I found a simple example on stabilizing, but as my orientation has changed (white dotted line), I know I should apply a rotation to the negative X axis. Unfortunately, I'm not sure how to do this math. The code I have commented below does stabilize the entire arrow, but it acts more like a compass pointed to true North (or in this case, positive X axis).

    so, how do I create a new vector3 that retains the rotations of Yaw and Pitch, but zeros the roll?


    Code (csharp):
    1.  
    2.  
    3.     public float gain = 3.0f;
    4.     public float easeOut = 0.25f;
    5.  
    6.     private Vector3 controlDirection, stabilizeVector;
    7.  
    8.     public virtual float Horizontal
    9.     {
    10.         get
    11.         {
    12.             return Input.GetAxis("Horizontal");  //L,R, A,D *yaw*
    13.         }
    14.     }
    15.     public virtual float Vertical
    16.     {
    17.         get
    18.         {
    19.             return Input.GetAxis("Vertical");  //U,D W,S *pitch*
    20.         }
    21.     }
    22.  
    23.     void Start()
    24.     {
    25.     //initialize
    26.         controlDirection = Vector3.zero;
    27.         stabilizeVector = Vector3.zero;
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.         controlDirection.Set(0, Horizontal, (Vertical * -1));  //grab input
    33.  
    34.         //if (controlDirection == Vector3.zero) //no input
    35.         //{
    36.         //    //correct banking
    37.         //    stabilizeVector = Vector3.Cross(transform.up, Vector3.up);
    38.         //    rigidbody.AddRelativeTorque(stabilizeVector * easeOut, ForceMode.VelocityChange);
    39.         //}
    40.  
    41.         rigidbody.AddRelativeTorque(controlDirection * gain, ForceMode.Acceleration);
    42.     }

    [edit] may have meant "roll" instead of bank [/edit]
     
    Last edited: Oct 20, 2013
  2. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    KarlGG likes this.