Search Unity

Nav shows PathComplete when clicking in the middle of the air

Discussion in 'Navigation' started by mostlyhuman, Apr 5, 2017.

  1. mostlyhuman

    mostlyhuman

    Joined:
    Mar 16, 2013
    Posts:
    53
    Need some ideas on how to troubleshoot this, this is called from another script where basically the user moves the mouse around on screen to choose where an action will take place and it constantly checks to see if where the mouse is hovering is reachable by the nav system, it works as intended if I am hovering on a platform whether it is reachable or not but if I move the mouse out into the open spaces (the gray areas in the screenshot) it will always show the path is complete when obviously there is no way to get there. The destination is assigned from the other script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class CharacterAI : MonoBehaviour
    7. {
    8.    public bool pathIsReachable = false;
    9.    public Transform destination;
    10.  
    11.    NavMeshPath path;
    12.  
    13.    void Start()
    14.    {
    15.       path = new NavMeshPath();
    16.    }
    17.  
    18.    public void CalculatePath()
    19.    {
    20.        NavMesh.CalculatePath(transform.position, destination.position, NavMesh.AllAreas, path);
    21.       print("Path is " + path.status);
    22.       if (path.status == NavMeshPathStatus.PathComplete)
    23.          pathIsReachable = true;
    24.       else
    25.          pathIsReachable = false;
    26.       print("pathIsReachable is " + pathIsReachable);
    27.    }
    28. }
     
    Last edited: Apr 5, 2017
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    the CalculatePath maps supplied path endpoints to the nearest navmesh positions - before the path is calculated. If you want to limit the mapping to a certain distance you can call SamplePosition with a radius that matches your snapping radius. before doing the path calulation - eg.:

    Code (csharp):
    1.  
    2. NavMeshHit hit;
    3. float snapRadius = 0.3f;
    4.  
    5. // Test that path end points are within snapping radius
    6. if (!NavMesh.SamplePosition(startPoint, out hit, snapRadius, NavMesh.AllAreas))
    7.   return false;
    8. if (!NavMesh.SamplePosition(endPoint, out hit, snapRadius, NavMesh.AllAreas))
    9.   return false;
    10.  
    11. /// Do path calculation code here ...
    12.  
     
  3. mostlyhuman

    mostlyhuman

    Joined:
    Mar 16, 2013
    Posts:
    53
    That worked perfectly, thank you so much, Jakob!
     
  4. delphinius81

    delphinius81

    Joined:
    Mar 6, 2012
    Posts:
    57
    I'm running into a similar issue, except I'm using the new component-based nav mesh system. It seems that SamplePosition only applies if you are using the component-less (old) NavMesh system. How do you use SamplePosition with the new component-based nav meshes. That is, I only want to sample the position for navmeshes associated with a specific agent type. Can this be done right now?
     
  5. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
  6. delphinius81

    delphinius81

    Joined:
    Mar 6, 2012
    Posts:
    57
    Excellent, thanks!