What would cause a NavMesh agent to compute an invalid path?
Asked Answered
C

3

17

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.

Agent can't find his way

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

  1. 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;
    }
    
  2. 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

  3. 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 );
        }
    }
    
  4. Disabling all my NavMeshObstacle on the entire scene (My agents don't have any NavMeshObstacle on them nor on their children)

  5. Adding more steps between the initial position and final destination

  6. Disabled the AutoRepath property of the agent

  7. 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.

Chong answered 14/1, 2017 at 10:6 Comment(3)
The problem is even more present since I've updated the project from Unity 5.3 to Unity 2017.1Chong
Can you create a Gizmo sphere in place of agent's destination and get a screenshot of it?Smectic
@Smectic : On the image I've attached to the question, the destination is represented by the red cone on the top-left corner. Sorry, it's not very visible.Chong
S
1

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

Sinusitis answered 23/1, 2017 at 11:58 Comment(1)
I thought about using the initial paths' corners so as to make my agent reach its destination but I had not enough time to implement it since it would compel me to rework my system. Making my agent "slide" along their path is not an option since I have many agents.Chong
S
0

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/

Smectic answered 20/11, 2017 at 7:51 Comment(3)
Thanks for your answer Fiffe. My destination points are far enough from the edges of my Navmesh to avoid this problem I think. Warping the agent will teleport him to its destination point, right? It's not really a convenient solution... Thanks for the link, I see that the Navmesh solution from Unity (especially since 2017) is not quite reliable. I may have to switch to an other solution. At least, for my future projects.Chong
Looking at the cone I'm pretty sure it's too small, I'm not quite sure what warping does as I didn't get to try it but I've seen people using it as a fix.Smectic
The image I took here was an example, but the problem occurs even if the destination point is in the middle of my Navmesh.Chong
H
0

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.

Herwin answered 22/3, 2018 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.