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)
{
collissions++;
}
}
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.