Search Unity

Failed to sample path position

Discussion in 'Navigation' started by phaem, Jul 14, 2017.

  1. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    Hello Unity community!

    I'm trying to sample path position from a running navmesh agent, but it constantly fails.

    Code (CSharp):
    1. var sampled = agent.SamplePathPosition(NavMesh.AllAreas, 0, out sample);
    The sampled flag is always false and the sample.hit flag is always false and sample.mask equals 1, wich means Unwalkable area. I've logged the agent's state at the moment of sampling, here's the output.

    Here you can clearly see the agent moving over Walkable area (mask 0) painted with blue.



    How do I get the area mask at the current agent's position?
     
  2. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
  3. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    But why sample.hit is false? How do I know it is valid?
     
  4. Jakob_Unity

    Jakob_Unity

    Joined:
    Dec 25, 2011
    Posts:
    269
    The hit bool refers to whether the position is blocked (areaMask) while scanning the path some distance ahead.
    See also the example code in the documentation
     
  5. phaem

    phaem

    Joined:
    Jan 5, 2015
    Posts:
    76
    Oh, I see. To make things clear. When using NavMeshAgent.SamplePathPosition, the resulting sample .hit flag shows if the position was blocked or sampled at distance requested. And when using NavMesh.SamplePosition, the resulting sample .hit flag shows if position on the navmesh found or not. This isn't stated in the docs here https://docs.unity3d.com/ScriptReference/AI.NavMeshHit.html, may be it should? Anyway, thank you for clarification :)