1
\$\begingroup\$

I have this script,

void Update () {

        float pitch, yaw, roll;

        pitch = Input.GetAxis ("Mouse Y");
        yaw = Input.GetAxis ("Mouse X");
        roll=Input.GetAxis ("Roll");


        transform.Rotate (-pitch, yaw, -roll);




        if (Input.GetKey (KeyCode.W))
            Rigid.AddRelativeForce (transform.forward * 50);
        else if (Input.GetKey (KeyCode.S))
            Rigid.AddRelativeForce (-transform.forward * 50);
        else if (Input.GetKey (KeyCode.D))
            Rigid.AddRelativeForce (transform.right * 50);
        else if (Input.GetKey (KeyCode.A))
            Rigid.AddRelativeForce (-transform.right * 50);
        else
            Rigid.velocity = Vector3.zero;

        if (Input.GetKey (KeyCode.Space)) {


            transform.rotation = Quaternion.Euler (Vector3.zero);
            Rigid.velocity=Vector3.zero;
            Rigid.angularVelocity=Vector3.zero;
        }

    }

My "Roll" is defined as 'q' and 'e' keys. "Rigid" is rigidbody object.

I simply want to implement 6DoF on this cube, the pitch, yaw and roll works fine , but when it comes to movement it behaves in very unusual way after the object is rotated, I tried moving by using localPosition still no result. I also used relative torque for rotation but it resulted in worse, that is why rotation is controlled by transform.rotation not torque.

I suppose I am not handling gimbal lock or something? Or am I doing it completely wrong?

\$\endgroup\$
  • \$\begingroup\$ Please define what you mean by a "very unusual way," ideally with images or video, and describe or show how this deviates from the behaviour that you want. If you don't tell us the symptoms, we can't help you diagnose the illness. \$\endgroup\$ – DMGregory Oct 12 '15 at 21:04
  • \$\begingroup\$ Here, the Video (dropbox.com/s/xzxfwws088on397/Rotation.3gp?dl=0) One more thing to note here is that, if it is moving with respect to global space, it is still not following the axes properly. \$\endgroup\$ – Talha Oct 13 '15 at 7:13
1
\$\begingroup\$

From the docs on Rigidbody.AddRelativeForce:

Adds a force to the rigidbody relative to its coordinate system.

So, you're accounting for the object's rotation twice:

  • once by using AddRelativeForce (which expects the force to be expressed in local space and transforms it for you)

  • and again by using transform.forward & right (which are worldspace vectors which already include the object's rotation).

This means the force vector is being rotated by the square of the object's rotation.

You can fix this by either sticking with AddRelativeForce and replacing transform with Vector3, or by keeping transform.forward/right but using Rigidbody.AddForce instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.