Pick a repeatable random destination on connected walkable polygons.
dotnet add package RecursiveAI.OnePathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Mesh.Queries; var rows = Enumerable.Repeat("......##......", 6); var mesh = NavMesh.Build2D( Grid.FromText(string.Join("\n", rows))); var query = new NavMeshQuery(mesh.Tiles); var start = query.FindNearestPoly( new(2,0,2), new(1,1,1)); var rng = new SeededRandom(42); var point = query.RandomPointAround( ref rng, start.Poly, start.Point, 10); Console.WriteLine($"Found: {point.Found}"); if (point.Found) Console.WriteLine($"Point: {point.Point}");
Found: True Point: (3.1335903772176454, 0, 1.4717821118105765)
Unzip the example, then dotnet run.
The full program
Each of the six grid rows is ......##......: dots are walkable cells and hashes are blocked cells. The two islands are disconnected. The radius reaches toward the other island, but the query starts on the left and expands through connected polygons only.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Mesh.Queries; var rows = Enumerable.Repeat("......##......", 6); var mesh = NavMesh.Build2D( Grid.FromText(string.Join("\n", rows))); var query = new NavMeshQuery(mesh.Tiles); var start = query.FindNearestPoly(new(2,0,2), new(1,1,1)); var rng = new SeededRandom(42); var point = query.RandomPointAround( ref rng, start.Poly, start.Point, 10); Console.WriteLine($"Found: {point.Found}"); if (point.Found) { Console.WriteLine($"Point: {point.Point}"); Console.WriteLine($"On starting island: {point.Point.X < 6}"); }
Found: True Point: (3.1335903772176454, 0, 1.4717821118105765) On starting island: True
Check Found before using the point: an invalid start, zero radius or exhausted sampling attempts returns no point.
Variations
Copy the generator before a draw to replay the same choice. Append this to the program:
var saved = rng; var first = query.RandomPointAround( ref rng, start.Poly, start.Point, 10); rng = saved; var replay = query.RandomPointAround( ref rng, start.Poly, start.Point, 10); Console.WriteLine(first.Found && replay.Found && first.Point == replay.Point);
TrueRelated recipes
The short answer
Find the starting polygon, create a SeededRandom, then call NavMeshQuery.RandomPointAround with that polygon, the center and the radius. Test the result’s Found property before using Point. It samples the portions of reachable polygons inside the circle instead of choosing arbitrary world coordinates and snapping them afterward.
The sample uses two islands so connectivity matters. Sampling all walkable polygons could select the other island even though it is close in straight-line distance. Starting the circle search from the left island excludes that disconnected surface.
How do you get a random point on a navmesh?
There are two useful scopes. RandomPoint samples every polygon accepted by the filter across the mesh. RandomPointAround first finds polygons through a circle search from a starting polygon and samples within the circle. Choose the latter for local wandering from a known position; choose the former when connectivity to the current character does not matter.
The query documentation specifies area weighting. A larger polygon receives proportionally more weight than a smaller one, measured by its X/Z outline area. Picking a polygon index uniformly would not have that property and would bias destinations toward finely divided parts of the mesh.
The returned height lies on the selected polygon’s surface. On stacked geometry, each found polygon counts separately. The query is not a method for choosing only the visually topmost floor under a point.
What is a random reachable point in a radius?
In this API, the circle search traverses connected polygons through portals that touch the circle. The final point lies within the circle in X/Z. The radius is not a path-length allowance: a winding route through those polygons can be longer than the straight-line distance from the center.
Use a start polygon associated with the character’s actual walkable surface. FindNearestPoly searches within supplied half-extents; if it finds no acceptable polygon, the reference is invalid and the random query returns no point. On multiple floors, choose vertical extents that do not accidentally snap the character to a different surface.
The sampler clips found polygons to the circle’s bounding square, draws from those clipped areas and rejects points outside the circle. After 64 failed attempts it returns no point. Zero radius and an invalid start also produce no point. A failed draw is a result to handle, not a default coordinate to send a character toward.
How do you make random navmesh movement repeatable?
Keep a SeededRandom instance and pass it by reference. Its state advances as the sampler draws. Recreating it with the same seed before every request repeats the first choice instead of producing a sequence. Copying and restoring the state repeats subsequent draws, as the variation demonstrates.
Repeatability also requires the same mesh, filter, start, center, radius and call sequence. Changing a door’s walkability changes which polygons can be reached; restoring only the random state cannot restore that geometry. Reacquire the start polygon after tile replacements because old references can become stale.
For a wandering character, retain the generator between destination requests, accept only successful samples, and then request a path to the chosen point. Keep “no destination found” separate from a legitimate destination at the coordinate origin. This avoids accidental movement caused by reading the result’s default Point when Found is false.