finding closest point in Navmesh with x free radius

Hello. I’m trying to place buildings in a Navmesh. We are using the NavMesh.SamplePosition to determine the closest point in the NavMesh, but we need to know if such point has available x radius to place an obstacle. Does anyone have a suggestion on an efficient way to achieve this?

Thanks!

Alejandro.

It’s not trivial, you need to sample multiple points.
Good feature request though!

Yeah, I figured, so I made a quick and dirty workaround and ended up doing something like this:

Vector3 spawnPoint = position;
Vector2 offset = Random.insideUnitCircle * 2;
spawnPoint.x += offset.x;
spawnPoint.z += offset.y;

NavMeshHit closestHit;
if (NavMesh.SamplePosition (spawnPoint, out closestHit, 20, 1)) {
spawnPoint = closestHit.position;
transform.position = spawnPoint;
agent.enabled = true;
NavMeshPath path = new NavMeshPath ();
agent.CalculatePath (spawnPoint, path);
agent.path = path;
}

Placed the unit on a random point along the radius and then mande a sample position to get the nearest point in the navmesh form such offset position…

You could use NavMesh.Raycast to find the distance to the navmesh edge in all the directions you care about. That’ll allow you to check if the exact position is what you want.

2 Likes