Search Unity

NavMesh.SamplePosition within specified area only.

Discussion in 'Navigation' started by kepler64b, Feb 3, 2019.

  1. kepler64b

    kepler64b

    Joined:
    Aug 9, 2017
    Posts:
    16
    So as per usual the Unity docs are very unhelpful. From my understanding NavMesh.SamplePosition function takes int areaMask which determines what areas to sample from, but it seem not to be working. Also it in the docs it says areaMask is a bitfield, so a custom layerMask 3 would be 1 << 3 = 8. But it still gets me a point within all NavMesh areas. Maybe I just miss interpreted what the function actually does?

    The code is dead simple, just an agent sampling a random point within specified range.
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    SamplePosition basically "projects" a given point onto a NavMesh and returns the projected point.
    If no point can be found with direct projection, it will simply look for the nearest possible point on any NavMesh, conforming to the areamask.
    How exactly is this "not working" for you?
     
  3. TomPo

    TomPo

    Joined:
    Nov 30, 2013
    Posts:
    86
    NavMeshQueryFilter filter = new NavMeshQueryFilter() { areaMask (1 << 4) | (1 << 5) };
    NavMesh.SamplePosition(pos, out NavMeshHit hit, 0.5f, filter)

    1<<4 - User 4 area
    1<<5 - User 5 area
    So is checking nearest point for these areas.
    So in general is checking if at a given vector (pos) specific areamask is present.
     
    Last edited: Apr 30, 2020