Hi everyone,

I’m trying to get a random point on a NavMesh that is 1) within a certain radius around a given point and 2) within a given area of the NavMesh. A little vizualisation: I want a random point within the circle around the purple Center Point that is on the green area.

Currently I’m achieving this with the following code that is attached to the Center Point.

```
// the radius paramter defines the size of the circle
public Vector3 GetRandomPosition (float radius)
{
Vector3 pos = transform.position;
Vector2 randomOffset = Random.insideUnitCircle * radius;
pos.x += randomOffset.x;
pos.z += randomOffset.y;
NavMeshHit navInfo;
// the LevelManager.Instance.AreaMask parameter defines the walkable area
if (!NavMesh.SamplePosition (
pos,
out navInfo,
float.MaxValue,
LevelManager.Instance.AreaMask))
{
// should never occur, but in case of an emergency
// return the position of the center point as this is
// confirmed to fulfill all constraints beforehand
return transform.position;
}
return navInfo.position;
}
```

Now, this code works and does what it’s supposed to do but there’s one problem, obviously. The bigger the Non Walkable area gets, the less evenly distributed are the returned positions as all initially generated random positions that area invalid get “shifted” towards the edge betweens Walkable and Non Walkable area by NavMesh.SamplePosition ().

I’ve found solutions on how to evenly distribute random positions on a NavMesh on Google but none of them took a search radius into consideration and so far I couldn’t come up with a solution on my own.

Does anybody have an idea how to do this?

Thank you in advance!