# 2d game - looking toward velocity for rigidbody player

Ok I am having a problem with rotating the Z axis of my player to face towards his movement.

Here is my code - note that my object is oriented with Vector3.up being his “front”:

``````desiredAngle = Vector3.Angle(Vector3.up, rigidbody.velocity);
if (dir != Vector3.zero)
{
if(dir.x > 0.1)
{
desiredAngle = -desiredAngle;
}
transform.eulerAngles = new Vector3(0, 0, desiredAngle);
}
``````

Now this works properly as far as the object facing as it should, problem is that when the player faces “negative desiredAngle” then the rigidbody “jerks” for only about 1 frame as it starts to turn to the right. This is ugly, and has got to have a better solution. Maybe a different way to calculate the angle, that wraps all the way around to 360 degrees, instead of just calculating the closest angle to a vector.

Or maybe I could do some smoothing somehow that would make the transition between a negative value for desiredAngle, and positive value, not noticeable? Rather than really twitchy when it switches over

OK let me start by saying I had to dig through the internet for DAYS to find this dang solution! but I did manage to solve my problem, by converting my vector 3 (my rigidbody.velocity) into a vector2 (the x and y of rigidbody.velocity), then convert that vec2 into a radian, then convert that radian into a degree, and finally, add 90 degree offset to allow my player object to exactly follow his velocity with only rotating his Z axis. Here is the code I used for those future question’ers who will certainly stumble all over unity answers trying to find this:

a million thanks to the guy who supplied this…only changes I needed to make were to use rad2deg rather than radtodeg like that says! fantastic! works flawlessly!