- Home /
NavMesh.SamplePosition returns point outside the NavMesh
Hi guys, I implemented a method for NavMeshAgent to run to random position on a NavMesh. To do that, i follow this guide.
However, the sampled position sometimes returns an invalid point, that point is outside navmesh. Sometimes far above the NavMesh, and sometimes below the NavMesh. Look at this picture:
The picture above shows us the sampled position returns a point below the Terrain. So, how to make the sampled position always on the Navigation Mesh and not outside the Navigation Mesh?
I'm using this code:
public Vector3 getRandomPointOnNavMesh(float walkRadius) { Vector3 randomDirection = Random.insideUnitSphere * walkRadius; randomDirection += transform.position; NavMeshHit hit; if (NavMesh.SamplePosition(randomDirection, out hit, walkRadius, 1 << NavMesh.GetNavMeshLayerFromName("Default"))) { return hit.position; } return getRandomPointOnNavMesh(walkRadius); }
Your answer
Follow this Question
Related Questions
Navmesh agent stuck on partial path 0 Answers
How to do AI pathfinding on trees and other vertical surfaces? 0 Answers
Make a flying enemy ? 0 Answers