Setting GridGraph Masks from Code

Hello there,

I’m really enjoying this fantastic A* tool so far, but I’ve run into a problem. Likely some beginner mistake on my end. I’ve got a dungeon that gets generated at runtime that I want to make a GridGraph for. It all works, except that the values I set for Obstacle Layer Mask and Height Mask seem to get ignored. Instead of what I want, I get a Mixed result with two layers: “TransparentFX” and “Layer3”.

My code looks like:

private void CreatePathfindingGrid()
    {
        // call this AFTER the 3d objects and everything have been generated. The pathfinding plugin
        // will then create the pathfinding map.
        AstarData data = AstarPath.active.data;

        GridGraph gg = data.AddGraph(typeof(GridGraph)) as GridGraph;

        int width = mapSize;
        int depth = mapSize;
        float nodeSize = 1;

        gg.center = new Vector3(0, 0, 0);

        gg.SetDimensions(width, depth, nodeSize);

        int obstaclesLayer = LayerMask.NameToLayer("Obstacles");
        gg.collision.mask = obstaclesLayer;

        int groundLayer = LayerMask.NameToLayer("Ground");
        gg.collision.heightMask = groundLayer;

        // make it half a node so that narrow corridors and things are walkable
        gg.collision.height = 0.5f;
        gg.collision.diameter = 0.5f;

        
        AstarPath.active.Scan();
    }

Which creates the grid just how I want it, but with the wrong layers. When I pause the game and check it in the inspector, the layers I specified aren’t set. What am I doing wrong? Pictures attached to explain better.

Hi

Those are bitmasks, not layer indices.
Use 1 << layerIndex instead.

Thanks! Works perfectly now

Hi Cheb,

I’m in the same difficulty you were, but I don’t understand Aron Granberg’s reply I understand about the bitmasks remark, and you needed to use layer indices instead, but I don’t understand what “Use 1 << layerIndex instead” means.

What did you do to fix the problem exactly?

Thanks for all your help!

@Fuzzy_top See Bitmask Tutorial - A* Pathfinding Project

Duh… I hadn’t thought about binary arithmetic since I was writing assembler code years ago… makes perfect sense… many thanks for all your excellent work! Next step: buy the Pro version!