Description of the problem
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)
- When instantiated, I ask my agents to compute their path to a given destination point on the NavMesh (I use NavMesh.SamplePosition to make sure the destination point is on the NavMesh). Everything works fine. The agent find his way and starts to move towards his target
- But, during is journey, suddendly, he loses himself while the NavMesh has not changed since the first step. I haven't asked him anything, no new computation of a new path.
Solutions tested
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
orSetDestination
. 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.