- Home /
Why does my navmesh agent take longest path? (VIDEO)
I'm trying to figure out why my navmesh agent would rather take the longest path to an object even if hes standing right next to it...
Does anyone know why this would be happening?
This is the navmesh after the first gate is opened
If I go to that left most gate first, he just opens it like the other, but then if I go to the top gate, he does the same thing... runs through the first opened gate to the outside of the gate he should have just ran straight to... no idea why
UPDATE: The gates have a Navmesh Obstacle on them: Per the docs "The Nav Mesh Obstacle component allows you to describe moving obstacles that Nav Mesh Agents should avoid while navigating the world" - I'm using a navmesh obstacle to alter the navmesh after a gate is opened - disabling the navmesh obstacle fixes the behavior - the problem is my player can just run through gates. I can stop movement in the trigger, but am wondering if there is a way to avoid it?
Answer by NoDumbQuestion · Jul 10, 2018 at 01:29 AM
Because some of area did not have blue area(walkable path)
In navmesh Navigation window(Bake Tab). Change radius of agent from 0.5f -> 0.25f or your agent size. Will fix your problem
Tried this and it didn't work, I tried going to 0.1 and same issue happens. I updated the video with a new link
also here: video
That far door. Have a range that is too close to the mesh.
When you click the gate. navmesh will find the closest point that is walkable. It will choose that point(the point on otherside of the gate) to go.
I would use SamplePath First. Then see if that point is walkable. Otherwise tell it just to move close to the destination as possible(different than just change final position).
Another option is to make your own path then apply it
Answer by digzelot · Jul 10, 2018 at 11:53 AM
The easiest solution for this was to manipulate the end point of the destination. I'm still not sure why, but It was like I was getting random results: will the agent open the gate on the correct side or will he run around the map to the other side?
Ultimately I just subtract 1 unit from the path towards the player, so that the destination was always on the side of the fence that the player is on:
destination = hit.transform.position;
distance = Vector3.Distance(transform.position, destination) - 1.0f;
moveToPos = (destination - transform.position).normalized * distance;
moveToPos = transform.position + moveToPos;
Thanks to @NoDumbQuestion for suggesting I build my own path
Your answer
Follow this Question
Related Questions
Navmesh Agent Not placed on mesh? 3 Answers
Graphics Problem and NavMesh Obstacles 0 Answers
Failed to create agent because it is not close enough to the NavMesh 0 Answers
How to prevent NavMeshAgents from colliding when warped to same position? 0 Answers
Why does my navmesh not stop at this wall? (stealth tutorial) 1 Answer