Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved NavMesh.FindClosestEdge is highly unreliable?

Discussion in 'Navigation' started by ecoude, Aug 19, 2020.

  1. ecoude

    ecoude

    Joined:
    Aug 15, 2019
    Posts:
    9
    I'm trying to use NavMesh.FindClosestEdge() if my click position is outside of my navmesh but it seems to only work 10% of the time (when my clicks are relatively close to an edge). Is there a factor that affect the search range for FindClosestEdge? Ideally the success rate of FindClosestEdge is 100%. Here's my code:

    Code (CSharp):
    1.     public void SetDestination(Vector2 destination)
    2.     {
    3.         NavMeshHit navHit;
    4.         if (!NavMesh.SamplePosition(destination, out navHit, 0.1f, NavMesh.AllAreas))
    5.         {
    6.             NavMeshHit newNavHit;
    7.             if (NavMesh.FindClosestEdge(destination, out newNavHit, NavMesh.AllAreas))
    8.             {
    9.                 this.destination = newNavHit.position; //this.destination is another vector2
    10.             }
    11.             else
    12.             {
    13.                 Debug.Log("Couldn't find an edge"); //This happens too often
    14.             }
    15.         }
    16.         else
    17.         {
    18.             this.destination = destination;
    19.         }  
    20.         CalculatePath();
    21.     }
     
  2. ecoude

    ecoude

    Joined:
    Aug 15, 2019
    Posts:
    9
    Nevermind, I hadn't read the "Locate the closest NavMesh edge from a point on the NavMesh." in the API and was trying to find it from a point that wasn't on the NavMesh. Added another NavMesh.SamplePos if my first one was false:


    Code (CSharp):
    1.     public void SetDestination(Vector2 destination)
    2.     {
    3.         NavMeshHit navHit;
    4.         if (!NavMesh.SamplePosition(destination, out navHit, 0.1f, NavMesh.AllAreas))
    5.         {
    6.             NavMeshHit preNavhit;
    7.             if (NavMesh.SamplePosition(destination, out preNavhit, 10f, NavMesh.AllAreas))
    8.             {
    9.                     this.destination = preNavHit.position;
    10.             }
    11.         }
    12.         else
    13.         {
    14.             this.destination = destination;
    15.         }
    16.         CalculatePath();
    17.     }
    Edit: actually, you can remove the entire FindClosestEdge function and just use SamplePosition since it'll find the closest position from the click (which is outside of the navmesh, hend finding an edge already).
     
    Last edited: Aug 19, 2020
    Angelorpheus and IndieFist like this.