Search Unity

[SOLVED] navMeshPath.status always returning complete

Discussion in 'Navigation' started by Murgilod, Aug 2, 2017.

  1. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    I'm following the answer to this answer here and I'm encountering a problem. No matter what, even if it's impossible for a path to be completed, my code is always returning true.

    Code (CSharp):
    1.  
    2.     bool CalculateNewPath()
    3.     {
    4.         navMeshAgent.CalculatePath(navigateTo, navMeshPath);
    5.  
    6.         if (navMeshPath.status != NavMeshPathStatus.PathComplete)
    7.         {
    8.             return false;
    9.         }
    10.         else
    11.         {
    12.             return true;
    13.         }
    14.     }
    By all accounts, this should be returning false when I click an area off the navmesh or on a part of the mesh that can't be traversed to, but it always returns true. What am I doing wrong here?
     
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    So it turns out the problem was actually *gasp* a bug with how Unity was presenting my navmesh, and the code works near perfectly. HOWEVER, I did need to tweak my code a bit to make it account for static objects that weren't a part of my navmesh. Here's what the new system looks like:

    Code (CSharp):
    1.     public bool CalculateNewPath()
    2.     {
    3.         bool navMeshPathStatus = false;
    4.         bool navMeshSamplePosition = false;
    5.  
    6.         navMeshAgent.CalculatePath(navigateTo, navMeshPath);
    7.  
    8.         if (navMeshPath.status != NavMeshPathStatus.PathComplete)
    9.         {
    10.             navMeshPathStatus = false;
    11.         }
    12.         else
    13.         {
    14.             navMeshPathStatus = true;
    15.         }
    16.  
    17.         NavMeshHit hit;
    18.         if (NavMesh.SamplePosition(navigateTo, out hit, 0.25f, NavMesh.AllAreas))
    19.         {
    20.             navMeshSamplePosition = true;
    21.         }
    22.         else
    23.         {
    24.             navMeshSamplePosition = false;
    25.         }
    26.  
    27.         if (!navMeshPathStatus || !navMeshSamplePosition)
    28.         {
    29.             return false;
    30.         }
    31.         else
    32.         {
    33.             return true;
    34.         }
    35.     }
    As you can see, I now use NavMesh.SamplePosition to determine if an object is actually a valid point on the navmesh. This has a few caveats for use cases outside of what I'm doing (for instance, it doesn't handle edges super well) but for the purposes of my RTS it works fine.
     
    MonkeyPuzzle and Johnny_1 like this.
  3. MonkeyPuzzle

    MonkeyPuzzle

    Joined:
    Jan 17, 2016
    Posts:
    119
    Thanks! Just what i needed.
     
  4. Hobby-Game-Developer

    Hobby-Game-Developer

    Joined:
    Aug 27, 2017
    Posts:
    14
    But there is a problem! SamplePosition only checks, if the destination point, is a valid point on the navmesh, not if an object is able to reach it. If you have a terrain with unreachable hight platforms, then those platforms are also a valid point on the mesh (if the area is baked blue), but if isolated, there is no way, a valid path can be calculated.