Reputation: 7356
I am struggling with my NavMesh Agents computing an invalid path while there is obvisously no reasons. The problem occurs from time to time when they are already moving with an initial valid path.
On the above image, the destination is the cone on the top left corner. (Don't mind the NavMeshAgent's direction arrow, I tried to move the agent by hand so as to try to "unlock" him)
Checked the destination is on the NavMesh
public Vector3 GetCharacterPositionOnNavMesh( Vector3 position )
{
NavMeshHit hit;
bool positionFound = NavMesh.SamplePosition( position, out hit, 500, NavMesh.AllAreas );
if ( !positionFound )
Debug.LogWarning( "No valid position found !" );
return positionFound ? hit.position : position;
}
Checked the area mask of my agents to make sure they can find a path to the destination despite the various areas of the NavMesh
Checking almost each frame if the agent's path is invalid. If so, compute a new one using CalculatePath
or SetDestination
. Sometimes, it works, sometimes not.
protected virtual void Update()
{
if ( !Running || !agent.enabled || !agent.isOnNavMesh )
return;
if ( !agent.pathPending && agent.path.status == NavMeshPathStatus.PathInvalid && Time.frameCount % 2 == 0 )
{
NavMeshPath path = new NavMeshPath();
agent.CalculatePath( CharactersManager.Instance.GetCharacterPositionOnNavMesh( finalDestination ), path );
agent.SetPath( path );
}
}
Disabling all my NavMeshObstacle on the entire scene (My agents don't have any NavMeshObstacle on them nor on their children)
Adding more steps between the initial position and final destination
Disabled the AutoRepath property of the agent
Computing the path, storing all the corners and setting the destination of my agent one corner at a time using a similar method to this one
Note : When another agent pushes my first agent, the latter seems to wake up and find its path.
Upvotes: 17
Views: 6011
Reputation: 39
If you can, just avoid of using NavMeshAgent system. We are ended up with tricks like auto-testing level passing ability between all the points of interest for whole level. We made some heuristics like jumping forward for a small steps to avoid problems. The Unity NavMesh system is completely broken because of approach they have choosen. They perform non-robust boolean operations for meshes using float32 and this resulted in degenerated triangles and huge number of the other problems. Don't use this, try to find the other proven solutions.
Upvotes: 0
Reputation: 1256
I've recently had a very similar problem where my NavMesh agents would get stuck and usually start spinning around when the destination was too close to the edge of the NavMesh. What fixed it for me was moving it away from the edge by half of agent's radius so when he reaches his destination he would be standing right on top of it.
If this doesn't help you - you can try warping
the agent:
https://forum.unity.com/threads/agents-get-stuck-on-navmeshlink.503527/ https://forum.unity.com/threads/agent-getting-stuck-in-another-agent-when-walking-in-corners-what-is-the-right-solution.501824/ https://forum.unity.com/threads/strange-navmeshsurface-behavior.501453/ https://forum.unity.com/threads/failed-to-create-agent-because-it-is-not-close-enough-to-the-navmesh.500553/ https://forum.unity.com/threads/navmesh-link-does-not-connect-properly-in-runtime.473223/
Upvotes: 0
Reputation: 306
I had the Same issue Sometime ago where my agent just keeps deteriorating form the main path as if it getting new information on the move,
Then i checked about it and get to know that it updates its path on the move always checking for new shortest distance to go through as you said that you have disable the auto Path property i did the same but no help.
Then i came up with the idea to Get the Points on which my agent initially starts moving in a list and draw my own path to move my Agent on You can get the points from this and after that u have to smoothly translate your player\agent in on those points as it will be the shortest path.
But last but not least obstacle avoidance can be hard to overcome
if you find this help full give it shot and tell how it goes. its just what i have did in my case thought sharing it would be helpful.
Good Luck Regards
Upvotes: 1