# Move Sprite in direction of rotation

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.

• possible duplicate of Simple thruster like behaviour when rotating sprite – Alex May 1 '15 at 20:43
• Nope. It is the opposite of that. I already have a thruster style movement. – coolblue2000 May 1 '15 at 20:44
• 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. – Alex May 1 '15 at 20:53

## 1 Answer

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