how to add torque to correct bank?

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:

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?

    public float gain = 3.0f;
    public float easeOut = 0.25f;

    private Vector3 controlDirection, stabilizeVector;

    public virtual float Horizontal
    {
        get
        {
            return Input.GetAxis("Horizontal");  //L,R, A,D *yaw*
        }
    }
    public virtual float Vertical
    {
        get
        {
            return Input.GetAxis("Vertical");  //U,D W,S *pitch*
        }
    }

    void Start()
    {
	//initialize
        controlDirection = Vector3.zero;
        stabilizeVector = Vector3.zero;
    }

    void FixedUpdate()
    {
        controlDirection.Set(0, Horizontal, (Vertical * -1));  //grab input

        //if (controlDirection == Vector3.zero) //no input
        //{
        //    //correct banking
        //    stabilizeVector = Vector3.Cross(transform.up, Vector3.up);
        //    rigidbody.AddRelativeTorque(stabilizeVector * easeOut, ForceMode.VelocityChange);
        //}

        rigidbody.AddRelativeTorque(controlDirection * gain, ForceMode.Acceleration);
    }

[edit] may have meant “roll” instead of bank [/edit]

well… this solution works to fix my problem: How to stabilize angular motion (alignment) of hovering object? - Questions & Answers - Unity Discussions

wish I understood Quaternions better…

1 Like