Support Forum

Tilemap walkable with A*

Hello,
I am working on a 2d game in isometric perspective. I have aligned my Tilemap with the A* Graph (as I saw somewhere in this forum) and it seems to have worked correctly. What I am trying now is to iterate through the Graph checking if each node is in the Tilemap, and defining if it is Walkable or not based on that. This is the code I am using (also taken partly from the examples):

public Tilemap groundTilemap;
    public Grid grid;

    void Start()
    {
        AstarPath.active.AddWorkItem(new AstarWorkItem(ctx => {
            var gg = AstarPath.active.data.gridGraph;
            for (int z = 0; z < gg.depth; z++)
            {
                for (int x = 0; x < gg.width; x++)
                {
                    var node = gg.GetNode(x, z);
                    Vector3 position = (Vector3)node.position;
                    Vector3Int currentCell = grid.WorldToCell(position);
                    var tile = groundTilemap.GetTile(currentCell);

                    node.Walkable = tile != null;
                }
            }

            // Recalculate all grid connections
            // This is required because we have updated the walkability of some nodes
            gg.GetNodes(node => gg.CalculateConnections((GridNodeBase)node));

            // If you are only updating one or a few nodes you may want to use
            // gg.CalculateConnectionsForCellAndNeighbours only on those nodes instead for performance.
        }));
    }

This is working to the extent that the area of the map that is not inside the Tilemap is not Walkable, while the area that is inside the Tilemap is Walkable. However, inside the Tilemap zone not all the nodes are becoming walkable, but they form this strange matrix:


The cell layout of the grid is configured as “isometric Z as Y”, and this is my Graph configuration:

My doubts are: am I iterating correctly through the Graph? Are the GetNode() and node.position functions used correctly here?
I’m not clear if I’m missing something obvious, since as I understand it if the cells are the same size and aligned in the exact same position this should work.

Thanks for your help.

Hmm, maybe your tilemap doesn’t actually contain tiles for all coordinates? Are the tiles bigger than 1x1 then the GetTile function might return null for some coordinates? Just speculating, but I think this is the most likely scenario. You code looks good from what I can see.

That was it Aron, I now check the nodes in a square group of four (as that my tile size) and it has solved it. I apologize because the problem wasn’t related to the A*, thanks for giving me a solution anyway.

Nevertheless, I still have one doubt. As I’ve said I fixed it, but the FPS rate has dropped from 35-40 to 10-14. Is this normal?

I’m using the settings above: a 300x300 grid in isometric perspective. There are around 30 agents in the map, and very few colliders.

This is the map after scanning:

Also, I have tried with a 150x150 grid and, without looping the nodes to make it walkable or not, I’ve seen the FPS rate is 55-60 (vs 35-40 in the same situation with the 300x300). Is 300x300 too big? Is there something I should be doing to optimize this? I’ve tried to change the thread count but it didn’t change the result.

Thanks for the help.

Hi

The fps drop could be due to rendering the graph in the scene view. Try to disable ‘Show Graphs’ in the A* Inspector and see if that improves things.

Hi Aron

Ok, now it is in 60-65 even with all the nodes configured. I didn’t expect such a change, thanks for the help!