Search Unity

How to know if a given destination point is valid before call SetDestination?

Discussion in 'Navigation' started by ptr0x, Mar 14, 2015.

  1. ptr0x

    ptr0x

    Joined:
    Dec 9, 2013
    Posts:
    54
    Hello,

    In the navmesh system is there a way to know if the destination point have a valid path before (or at least in the same frame) a call to NavMeshAgent.SetDestination?

    I send a network packet when the client moves around the map, but I can only send the packet if the movement is valid.

    Thanks.
     
  2. Smithy43

    Smithy43

    Joined:
    Sep 8, 2012
    Posts:
    11
    In order to determine if an agent can navigate to a position I think you have two options:
    1. Use NavMesh.CalculatePath to calculate a path between the source and destination points. This happens synchronously so you get the path back immediately and you can therefore determine its status without waiting (you can then set this path on the agent if you wish using NavMeshAgent.SetPath)
    2. Use NavMeshAgent.SetDestination and then wait until the path has been calculated to determine if it is valid or not (you can use a coroutine and wait until !NavMeshAgent.PathPending and then check NavMeshAgent.pathStatus once completed)
    I think the main benefit of option 2 is that the path is calculated in a separate thread so you may get a performance benefit.
     
  3. ptr0x

    ptr0x

    Joined:
    Dec 9, 2013
    Posts:
    54
    Thank you very much.
     
  4. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    magique likes this.