2
\$\begingroup\$

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

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

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); 
\$\endgroup\$
  • \$\begingroup\$ I am using nevMesh will it allow me to get velocitiy? \$\endgroup\$ – Muhammad Faizan Khan Jun 27 '16 at 12:30
  • \$\begingroup\$ 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. \$\endgroup\$ – jgallant Jun 27 '16 at 12:31
  • \$\begingroup\$ 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 \$\endgroup\$ – Muhammad Faizan Khan Jun 27 '16 at 12:32
  • \$\begingroup\$ 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) \$\endgroup\$ – jgallant Jun 27 '16 at 12:34

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.