Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 468
Hellium Avatar asked Jan 14 '17 10:01

Hellium


People also ask

What is NavMesh agent?

The NavMesh Agent component helps characters to avoid each other, move around the scene toward a common goal, or any other type of scenario involving spatial reasoning or pathfinding. Once a NavMesh has been baked for the level, it is time to create a character which can navigate the scene.

What is NavMesh surface?

The NavMesh Surface component represents the walkable area for a specific NavMesh Agent type, and defines a part of the Scene where a NavMesh should be built. It is not in the Unity standard install; see documentation on high-level NavMesh building components for information on how to access it.

Is NavMesh AI?

A navigation mesh, or navmesh, is an abstract data structure used in artificial intelligence applications to aid agents in pathfinding through complicated spaces. This approach has been known since at least the mid-1980s in robotics, where it has been called a meadow map, and was popularized in video game AI in 2000.


1 Answers

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

like image 70
Syed Anwar Fahim Avatar answered Oct 21 '22 13:10

Syed Anwar Fahim