# Unity How do you rotate an object with a rigidbody so it's facing the it is traveling [duplicate]

I would like to know how i can rotate an object with an rigidbody so it's facing the way it's traveling.

• What did you try? Jun 12, 2015 at 11:04

First you need to get the direction you are traveling in:

Vector2 direction = rigidBody2D.velocity.normalize();


Then you want to get the angle of this direction:

float ang = Vector2.Angle(rigidBody2D.transform.position, direction);
Vector3 cross = Vector3.Cross(rigidBody2D.transform.position, direction);

if (cross.z > 0)
ang = 360 - ang;


Quaternion q = Quaternion.Euler(ang);
rigidBody2D.MoveRotation(q);


I didn't test any of this.

• nice @jon ... it's the simple stuff that I often trip up on lol
– War
Jun 12, 2015 at 12:37
• lol @Wardy, this wasn't your question though :P Jun 12, 2015 at 12:37
• Yeh I just liked how you went ... "oh it's easy ..." and i'm sat here thinking ... aww this is gonna be a tough one lol
– War
Jun 12, 2015 at 13:07
• I'm sorry i didn't specify it in the question but how would you do this in a 3d space with a normal rigidbody and not a rigidbody2D. Jun 12, 2015 at 18:49
• Same thing, except to get the angle you would do: Vector3.Angle(rigidBody.transform.position, direction); Jun 12, 2015 at 18:51