# How to follow a car with correct rotation

I am making a game where car will follow a user car. I am moving use car using keyboard keys it is working fine running on my game environment roads smoothly. To follow enemy car i am using nevMesh, it correctly following but the following car/enemey car rotation is not correct. I am surprise that how do i control it? or is there any other way to acheive this thing

• Look into Steering Behavior. gamedevelopment.tutsplus.com/series/… – jgallant Jun 27 '16 at 11:26
• thanks man for helpful link it looks so hard is there any easy way? – Muhammad Faizan Khan Jun 27 '16 at 11:35
• Yeah, you should more than likely just make the rotation always go in the direction of the velocity vector of that object. – jgallant Jun 27 '16 at 11:40

## 1 Answer

Make the gameobject face towards is velocity vector (or its last non-zero velocity vector):

 float angle = Mathf.Atan2(rigidbody2D.velocity.y, rigidbody2D.velocity.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), rotateSpeed * Time.deltaTime);

• I am using nevMesh will it allow me to get velocitiy? – Muhammad Faizan Khan Jun 27 '16 at 12:30
• Your navMesh has no concept of velocity no. You will need to get that from the gameObject you are trying to rotate. More then likely you have attached a rigidBody component to this gameobject, which provides you with a velocity. – jgallant Jun 27 '16 at 12:31
• if i add rigidbody to my enemey object then, i guess i will still not get the velocity as nevMesh propbably not move object using rigidbody – Muhammad Faizan Khan Jun 27 '16 at 12:32
• If you are not using a rigidBody to move your entity, then consider interpolating its movement between frames. If you know position1, position2, and the time it took to get there, you can easily deduce the objects velocity with simple physics calculations. You don't even need to know the velocity, you only need to know that last directino of movement really, so all you need to do is (Position2 - Position1) – jgallant Jun 27 '16 at 12:34