How to find obstacles on a 2D Tilemap based on a variable?

Hi!

I’m having a hard time figuring out how to implement this Pathfinding system into my 2D top down game. The map tiles are consisting of sprites and the Tile script, which holds the “Walkability” logic, based on it’s type/sprite name (i.e.: if sprite.name = “wall” -> tile.walkable = false).

Is there a way to make Pathfinding Project find the “walkable” variable and take it into account on path creation? Or a way to use the tiles instead of its own nodes?

I’m using the Pro version, in case it’s important.

Thanks!

Hi

You will have to copy the information to the pathfinding grid. It would look something like this

AstarPath.RegisterSafeUpdate(() => {
    var graph = AstarPath.active.astarData.gridGraph;
    
    for (int z = 0; z < graph.depth; z++) {
        for (int x =0; x < graph.width; x++) {
            var index = z*graph.width + x;
            var node = graph.nodes[index];
            node.Walkable = ... (your code here)
        }
    }

     // Needed after walkability has been updated to ensure everything is up to date
     AstarPath.active.FloodFill();
}

Alternatively you could subclass the GraphUpdateObject, which would also allow you to do smaller updates of the graph

public class MyGUO : GraphUpdateObject {
    public void Apply (GraphNode node) {
         var gnode = node as GridNode;
         gnode.Walkable = ... (your code here)
    }
}

var guo = new MyGUO();
guo.bounds = new Bounds(Vector3.zero, Vector3.one*1000);
AstarPath.active.UpdateGraphs(guo);