Search Unity

SamplePosition does not really get the nearest point on Navmesh...

Discussion in 'Navigation' started by Bastienre4, Aug 8, 2017.

  1. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    Hello !

    I'm using the Navmesh.SamplePosition to get the nearest position on Navmesh. However, it does not really get the nearest point on Navmesh... It looks like its discarding the Y position in the distance calculation.

    Is this a bug ? Any idea how to find a workaround ?

    Thanks ! :)
     
  2. DwinTeimlon

    DwinTeimlon

    Joined:
    Feb 25, 2016
    Posts:
    300
    What is your maxdistance when calling SamplePosition? If you make it small enough it might work.
     
    Bastienre4 likes this.
  3. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    Thanks for yout answer. Making it small is the workaround I found yesterday but i'm not happy with it. SamplePosition should give the nearest position no matter the search distance, isnt it ? :).
     
  4. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    Nobody ?
     
  5. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Regarding the documentation:
    It does function as it supposed to.
    I think what you wanted is a mix between NavMesh.SamplePosition and NavMeshAgent.SamplePathPosition where the maximun distance is regarding the path distance. Is there correct?

    Hope I could answer you!
     
    Bastienre4 likes this.
  6. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    Oh, I didnt saw that part in the documentation...
    Thanks a lot, that makes sense now.
    Actually, I'm trying to get the nearest position inside the NavMesh of one point in world space. Isnt there any solution for that ?
     
  7. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    What you mean? A random nearest point within a range?
     
    Bastienre4 likes this.
  8. Bastienre4

    Bastienre4

    Joined:
    Jul 8, 2014
    Posts:
    191
    No :p. Given a point in world space, I want to get the nearest point inside the NavMesh.
     
  9. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    Maybe someone can get you a result you expect, because for me nearest point is relative according to the point of view. I have a script that i give a range and it returns a random position within this range. I could say that your point could be it self but added just position.x += 0.001f; and it still would be the nearest point you looking for.

    Unless that your point is outside the NavMesh bounds. If it is a NavMeshAgent object, you could check for the navMeshAgent.isOnNavMesh() to check whether it is inside the navmesh bounds. If not, then you execute navMeshAgent.FindClosestEdge() for an agent or NavMesh.FindClosestEdge() for any object position. Then you will get it inside.

    that's why I made the question on the last post.
     
    Bastienre4 and DwinTeimlon like this.