A* Pathfinding Project

Cannot seem to scan graph from script


#1

Hi,
I’m procedurally generating a 2D tilemap and want to scan the graph after having done that.
However, it doesn’t quite appear to work.
I’m using AstarPath.active.Scan() after having generated the tilemap, however, it shows the entire map as blue. If I then simply hit the scan button in the unity editor, it does what I expected.

Should I not be able to simply scan from script?
My grid graph has been set up in the editor, and I’m I just set the size and ask it to scan, in my script.

Code:

void GeneratePathGraph(int width, int height)
    {
        // get graph
        var gg = AstarPath.active.data.gridGraph;

        float nodeSize = 0.5f;
        gg.center = new Vector3(width / 2f, height / 2f, 0);

        // Updates internal size from the above values
        gg.SetDimensions(width * 2, height * 2, nodeSize);

        // Scans graph
        AstarPath.active.Scan(gg);
    }

#2

Hi

If you are creating a bunch of colliders it is possible you might have to wait for a frame or explicitly tell the physics system to sync everything. Otherwise when the graphs are being scanned it may not pick up the new colliders.

See also https://docs.unity3d.com/ScriptReference/Physics.SyncTransforms.html


#3

That fixed it, thanks!

I’ve put a WaitForEndOfFrame() in before scanning, and it all seems to work now.
Is there a smarter way, to detect that colliders are ready, other than waiting a fixed time? Any events maybe?


#4

Hi

You could try to call Physics.SyncTransforms (linked above). Right before you scan. That might be enough.


#5

Couldn’t quite make a SyncTransorms call work. Oh well, waitForEndOfFrame seems to do the trick.

Thanks again.