jdavies
#1
I thought I saw somewhere on the forum that I should be able to do this…
var gridGraph = AstarPath.active.data.gridGraph;
pathTarget.position = PathUtilities.GetPointsOnNodes(gridGraph.nodes.Where(node => node.Walkable).ToList(), 1);
It makes since since GridNode inherits from GraphNode, but I’m not really sure what’s going on. Maybe an older version of the project?
Anyways, please help me extract a single random point from a grid graph.
Hi
I think your code just misses a cast and to extract the first index from the list that is returned (I haven’t tested it, but I think this should work)
var gridGraph = AstarPath.active.data.gridGraph;
var nodes = gridGraph.nodes.Where(node => node.Walkable).OfType<GraphNode>().ToList();
pathTarget.position = PathUtilities.GetPointsOnNodes(nodes, 1)[0];
For a faster solution you can use:
var gridGraph = AstarPath.active.data.gridGraph;
var randomNode = gridGraph.nodes[Random.Range(0, gridGraph.nodes.Length)];
pathTarget.position = randomNode.RandomPointOnSurface();
You might also be interested in this tutorial on making a wandering ai (linking to the beta docs as this tutorial has not been added to the release docs yet): https://arongranberg.com/astar/documentation/dev_4_1_6_17dee0ac/wander.php
jdavies
#3
Excellent! Yep, just wasn’t casting it… Thanks for answering this… Feel kind of silly now…