How can I get a random point on the GridGraph?

Hi

You can do

```
using System.Linq;
...
var gg = AstarPath.active.astarData.gridGraph;
List<Vector3> somePoints = PathUtilities.GetPointsOnNodes(gg.nodes.Where(node => node.Walkable).ToList(), 1);
```

Which will pick a single point completely uniformly on the whole graph.

However it is a bit slow since it needs to go through all nodes on the whole graph. A faster approach is something like

```
var gg = AstarPath.active.astarData.gridGraph;
for(int i = 0; i < 1000; i++) {
var node = gg.nodes[Random.Range(0, gg.nodes.Length)];
if (node.Walkable) {
return node.position;
}
}
// Could not find a point after 1000 tries, is the whole graph unwalkable perhaps?
return Vector.zero;
```

Hi I’m looking for similar functionality, however I need to get a random point within a radius of the current agent.

For example the agent is standing at (0,0,0) and I want to find a random traversable point greater than 2 units, but less than 1 unit. Is there any helper functions for this or will I need to iterate over the Grid Graph and find all points within my radius first and then randomly select one of those points.

I’m thinking the code would look something like:

```
public static Vector3? GetRandomPointWithinDistanceRangeOfPosition(Vector3 position, int maxDistance, int minDistance)
{
var gg = AstarPath.active.data.gridGraph;
List<Vector3> somePoints = PathUtilities.GetPointsOnNodes(gg.nodes.Where(node => node.Walkable && Vector3.Distance((Vector3)node.position, position) > minDistance && Vector3.Distance((Vector3) node.position, position) < maxDistance).ToList(), 1);
return somePoints.Count > 0 ? somePoints[Random.Range(0, somePoints.Count - 1)] : null;
}
```

This doesn’t work though because GetPointsOnNodes expects the first param to be a list of GraphNode where the linq expression returns a list of GridNodes. This is weird because it looks like GridNodes ultimately inherit from the GraphNode.

Any tips would be appreciated.

P.S. I spun my own A* library, but couldn’t quite figure out RVO. This asset is awesome!

Hi

Take a look at https://arongranberg.com/astar/docs/wander.html which goes through many methods for achieving similar things.

Thanks! My fault. I should have paid closer attention to the docs.