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

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 ! :slight_smile:

What is your maxdistance when calling SamplePosition? If you make it small enough it might work.

1 Like

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 ? :).

Nobody ?

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!

1 Like

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 ?

What you mean? A random nearest point within a range?

1 Like

No :p. Given a point in world space, I want to get the nearest point inside the NavMesh.

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.

2 Likes