- Home /
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?
Answer by Barrrettt · Apr 25, 2017 at 05:13 AM
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.
Answer by chadwickhynes · Nov 09, 2020 at 02:42 PM
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
Answer by RiQQ92 · Apr 14, 2016 at 12:10 PM
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.
Answer by kevojo · May 12, 2017 at 05:28 PM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612074743im_/https://answers.unity.com/themes/thub/images/avi.jpg)