A* Pathfinding Project

Find all traversable nodes inside a defined area


I am trying to find all traversable nodes (nodes that did not encounter a collission) in a certain area (area is defined by a PolygonCollider2D). I created this method to do that:

private void GetAvailableSpawnPositions()
        var bounds = _collider.bounds;

        int minX = (int)bounds.min.x;
        int minY = (int)bounds.min.y;
        int maxX = (int)bounds.max.x;
        int maxY = (int)bounds.max.y;
        for(int y = minY; y < maxY; y++)
            for (int x = minX; x < maxX; x++)
                RaycastHit2D [] hits = Physics2D.RaycastAll(new Vector2(x + 0.5f, y + 0.5f), Vector2.zero, 0, colliders);
                int collissions = 0;

                foreach(RaycastHit2D hit in hits)
                    if (hit.collider != null)

                if(collissions <= 0)
                    availableSpawnPositions.Add(new Vector3(x + 0.5f, y + 0.5f, 0));

I now realized I’m just doing exactly what Astar scanning is doing, only that I’m doing it inside my defined area.
Is there any way I can just use the node information already available after the Scan to do something like this?

I.e find all nodes that are traversable inside my defined area, and add those positions to a List of Vector3 positions.



I think something like this should work fine:

List<Vector3> points = new List<Vector3>();
AstarPath.active.data.graphs[0].GetNodes(node => {
    if (node.Walkable) points.Add((Vector3)node.position)

You may also be interested in this documentation page: https://arongranberg.com/astar/docs/wander.html


That would indeed do it, except this way I am looking at the entire graph, and not a defined area like above. Can you think of any way to do that?

EDIT: Nvm Ill figure it out :slight_smile:


You could add a filter like

if (_collider.bounds.Contains(point))

If you are using a grid graph you can also use the method GridGraph.GetNodesInRegion.

var nodes = AstarPath.active.data.gridGraph.GetNodesInRegion(_collider.bounds);