Weird behavior / bug when scanning a layered grid graph

Hello,

Im scanning my layered grid graph through script with the following script:

    private void CreateAiNavGrid(){
        LayerGridGraph gg = AstarPath.active.data.AddGraph(typeof(LayerGridGraph)) as LayerGridGraph;
        gg.center = Vector3.zero;
        gg.SetDimensions(chunkSize * grid.gridDimension, chunkSize * grid.gridDimension, cellWidth);
        gg.nodeSize = 5;
        gg.characterHeight = 5;
        gg.maxStepHeight = 5;
        gg.maxStepUsesSlope = true;
        gg.collision.diameter = 1f;
        gg.collision.fromHeight = 1000f;
        gg.cutCorners = false;
        gg.collision.mask = LayerMask.GetMask("AIObstacle");
        AiNavGridRule rule = new();
        rule.wg = this;
        gg.rules.AddRule(rule);
        StartCoroutine(ScanGraphAsync());
    }

I have a pre-placed house in the scene. when the scan occurs after the entire terrain and trees are placed, the house gets scanned completely butchered. If i hit manually scan after in the editor nothing happens. However if i move the house even a small tiny fraction of a margin and hit scan, the scan is no longer weird. Similarily, if i tweak a small change in the graph using the editor (for example enabling cutting corners or changing character height from 5 to 4.9 or changing collision diameter from 1 to 0.9) and then hit scan it also fixes it.
Heres a video showing whats happening: Imgur: The magic of the Internet
Why is this happening? and how can I fix this?

Is there a specific reason you’re scanning your graph using ScanGraphAsync? You have a reference to the graph object (gg), maybe try just calling Scan against it, and see if that makes a difference.

Not sure if you’re updating anything during runtime that affects your graph, such as moving objects that belong to the collision mask, but if you are, then you may want to call Physics.SyncTransforms to sync up any attached Colliders or Rigidbodies. I procedurally build out my graphs, and the rooms they occupy. After building out the rooms, but before scanning the graphs, I’ll call SyncTransforms to ensure that the attached Colliders have been properly synced up, and reduce the likelihood of the types of problems you’re describing.

In the interest of full disclosure, there’s a post on the Unity forums by one of the physics devs making the case for not needing to call SyncTransforms.

Hi

I note that you are using SetDimensions with nodeSize=cellSize, but then you immediately overwrite the nodeSize field. I can’t say why this would cause something to get messed up, but it might be good to check at least?
Other than that, I cannot see anything strange with your code.

However, I see that the house has some very very narrow colliders on it. The grid graph uses a raycast to figure out where the ground is. If some raycasts hit those railings, and some don’t, then some nodes will end up on the railings and some will be below it, which might look like what you are seeing.

Hey

Thanks for you reply. I took some time off developing for the past few days as I was busy with school work.
Turns out you were right for some reason over writing the nodeSize after using the SetDimensions method caused this weird behavior. I removed that and left SetDimensions and edited the third parameter and it seems like its fixed now. I have no idea what caused this but maybe it was an error on my side assigning the width and depth and node size.

Thanks for the help @aron_granberg and @krice ^-^

1 Like