Instantly Turn with Nav Mesh Agent

I’ve been testing Nav Mesh Agent and i really like it because of it’s auto route system, but i can’t get a good movement system with it.

I’ve tried a lot of values but i can’t get a responsive movement for it, Specially when i set a new position while it’s moving, doing some kind of ice skating and drifting to the next destination.

I would like to Instantly turn the navigator to the new directlion i clicked the character to move to (It’s a click and move game), But i can’t manage that by changing values in the inspector.

Do i have to do a code just to achieve that or is it a proper way to do so?

4 Answers

4

The solution for me:
Manually control the rotation.

public class AgentControler: MonoBehaviour {

private NavMeshAgent agent;
private const float rotSpeed= 20f;

void Start() {
    agent = GetComponent<NavMeshAgent>();
    agent.updateRotation = false;
}

void Update() {
    InstantlyTurn(agent.destination);
}

private void InstantlyTurn(Vector3 destination) {
    //When on target -> dont rotate!
    if ((destination - transform.position).magnitude < 0.1f) return; 
    
    Vector3 direction = (destination - transform.position).normalized;
    Quaternion  qDir= Quaternion.LookRotation(direction);
    transform.rotation = Quaternion.Slerp(transform.rotation, qDir, Time.deltaTime * rotSpeed);
}

//note: We can return the rotation control to the agent when it moves. for example:

agent.updateRotation = ((agente.velocity).magnitude > 0.2);

//When there are more agents or obstacles is interesting.

This one worked very well thank you! But if you have an issue with your player model with this code, that it seems like it is stutttering while moveing to the destination just add and FixedUpdate() for it and call InstantlyTurn() there. This worked as solution for me!

Hello from the future! Just wanted to say thanks for this code snippet, the function works really well in my project. Also shout out to TowisGameLab for the FixedUpdate tip. Both of you are legends!

I did the opposite of what others suggested, jack the acceleration way up (I did 999, though I didn’t find the min value to make it work).

Speed = 20 (whatever you want here)
Angular Speed = 999
Acceleration = 999
Stopping Distance = 0
Auto Braking = True

That seemed to work for me

Amazing, this worked for me haha

> Make sure that there are no compile > errors and that the file name and > class name match. This error seems to be related to another script you have thats trying to access a script named UIManager in order to add it to an object. So your issue is with another script in your project, not this one ive created. Create a new scene real quick to test on and drag the script here to an empty gameobject and press Play. Be sure to assign the variables that are in the inspector so you can test properly. Let me know what happens.

You're a true hero, thank you!

Best approach would probably be, is to write your own little method to turn your object towards NavAgent’s first turn point.

First use NavAgent to calculate the path and then use the path’s waypoints to determine where your object should turn, after your turning method send the agent on its way.

You can always turn the rotation speed of the navmeshagent way up: the faster this is, the faster it rotates towards destination. This combined with turning acceleration down a bit might be a simple way to solve this problem. if the acceleration is low, it will take longer for the object to get moving, this was how I personally fixed the issue.

With regards to animation, you might want to make a check that the object is moving a certain speed (check the vector3.distance(newpos, oldpos) ) before playing something like a walking animation. Maybe something like:

float distance = vector3.distance(newpos, oldpos); if (vector3.distance(navmeshagent.destination.point, gameobject.transform.position) > 0.5f && distance < 0.5f){ animation.play("turning"); } else if (vector3.distance(navmeshagent.destination.point, gameobject.transform.position) > 0.5f && distance < 0.5f){ animation.play("running"); } else animation.play("idle");

I’m still trying to figure out a way to find which absolute direction the navmeshagent is rotating in. Hard because when it rotates 360 degrees it switches from negative to positive, visa versa.

anyways hope this helps!