0
\$\begingroup\$

I have the following code.

void Update () {

        if (Input.GetAxis("Vertical")!=0)
        {
            GetComponent<Rigidbody2D>().AddForce(-transform.right);
        }

        if(Input.GetAxis("Horizontal")>0)
        {
            GetComponent<Rigidbody2D>().transform.Rotate(Vector3.forward * rotationSpeed);
        }

        if (Input.GetAxis("Horizontal") < 0)
        {
            GetComponent<Rigidbody2D>().transform.Rotate(Vector3.forward * rotationSpeed*-1);
        }
    }

This obviously moves my sprite in an asteroids fashion. What I really want is the sprite to track the direction it is facing (ie like a car) so as you rotate the sprite it continues its movement in the direction it is facing rather than the initial direction the force was applied.

\$\endgroup\$
  • \$\begingroup\$ possible duplicate of Simple thruster like behaviour when rotating sprite \$\endgroup\$ – Alex May 1 '15 at 20:43
  • \$\begingroup\$ Nope. It is the opposite of that. I already have a thruster style movement. \$\endgroup\$ – coolblue2000 May 1 '15 at 20:44
  • \$\begingroup\$ Apologies, I'm tired and I completely misread your question. This question might help you though (it's about moving towards the mouse, but a similar concept). There's also some linked questions on the right of that question that may also be helpful. \$\endgroup\$ – Alex May 1 '15 at 20:53
2
\$\begingroup\$

When you rotate the transform object, rotate the velocity vector by the same amount.

\$\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.