Support Forum

How to move LayerGridGraph vertically?

Hi,

I have a procedurally generated building that generates new floors at runtime as my camera pans up the building. I’m using a LayerGridGraph so that each visible floor of the building can have agents moving around, and I’d like to be able to move my graph upwards/downwards following the camera without constantly re-scanning the whole graph, so that newly generated floors can have pathfinding.

I was hoping to use ProceduralGridMover for this, but I didn’t realize at first that that class only supports moving a graph on the XZ axis. Is it possible to move a LayerGridGraph vertically without rescanning the whole graph, and if so do you have any suggestions for how to do this?

Thanks!

Hi

I’m afraid that moving it upwards does require rescanning the whole graph. You definitely need to do a raycast for each cell since it’s moving upwards and new obstacles can appear at any cell. And then you might as well just recalculate everything.
Doing a graph update which covers the entire graph may be faster than rescanning the graph though since it can avoid re-allocating some nodes.

Also you may want to use the beta version since the performance of scanning layered grid graphs has been greatly improved there.
See https://www.arongranberg.com/astar/download

You can move the graph by changing the center parameter of the graph.

var graph = AstarPath.active.data.layerGridGraph;
graph.center += Vector3.up;
graph.UpdateTransform();

// Do update here
1 Like

Hi Aron, thanks for your quick reply!

I’ll try out the beta version and see how much that improves performance.

To do a graph update from a script (rather than using the GraphUpdateScene component), would it look something like this?

var graph = AstarPath.active.data.layerGridGraph;
graph.center += Vector3.up;
graph.UpdateTransform();

// Do update here
Collider coll = GetComponent<Collider>(); // assume this collider's bounds cover the whole graph
Bounds bounds = coll.bounds;
GraphUpdateObject guo = new GraphUpdateObject(bounds);
guo.updatePhysics = true;
AstarPath.active.UpdateGraphs(guo);

And would I need to run this whole block of code as a new AstarWorkItem via AstarPath.active.AddWorkItem()?

Yes that looks good.
You might also want to add

AstarPath.active.FlushGraphUpdates();

to force the update to happen immediately instead of whenever it’s convenient for the pathfinding system.

You do not need to wrap that in a AddWorkItem. I mean technically the first part should be, but no pathfinding code uses those properties, so whatever.

1 Like

Great, thanks so much for your help!