Moving a graph at runtime
How to move a graph at runtime.
Why would I want to move a graph at runtime?
There are several reasons for why you might want to move a graph at runtime:
You have loaded a graph from a file, but it is not quite where you want it to be placed. You want to move the whole world to a new position, and you want the graph to move with it. You want to move and resize the graph to cover, for example, a procedurally generated level, and then scan the graph at the new position. You want to move the graph to follow an agent around in a very big, or even infinite, procedural world.
See
If you want to create a graph from scratch, take a look at Creating graphs during runtime.
How do I move a graph at runtime?
The way to move a graph depends on the graph type, but you'll generally first assign new settings to the graph, and then use the RelocateNodes method on the graph to actually move the nodes. Some graphs have an overload of the RelocateNodes method, which allows you to specify the orientation settings directly.
The RelocateNodes method has a few different overloads, but the most common usage can be seen in the code snippets below:
For the grid graph/layered grid graph:
// Move the graph to the origin, with no rotation, and with a node size of 1.0
For the recast graph:
var gg = AstarPath.active.data.gridGraph;
gg.RelocateNodes(center: Vector3.zero, rotation: Quaternion.identity, nodeSize: 1.0f);
// Move the graph to the point (20, 10, 10), rotated 45 degrees around the X axis
For the navmesh graph:
var graph = AstarPath.active.data.recastGraph;
graph.forcedBoundsCenter = new Vector3(20, 10, 10);
graph.rotation = new Vector3(45, 0, 0);
graph.RelocateNodes(graph.CalculateTransform());
// Move the graph to the point (20, 10, 10), rotated 45 degrees around the X axis
var graph = AstarPath.active.data.navmesh;
graph.offset = new Vector3(20, 10, 10);
graph.rotation = new Vector3(45, 0, 0);
graph.RelocateNodes(graph.CalculateTransform());
Scanning a graph at a new position
If you want to move a graph and then scan it at the new position, you can insert a Scan call at the end:
// Move the graph to the origin, with no rotation, and with a node size of 1.0
var gg = AstarPath.active.data.gridGraph;
gg.RelocateNodes(center: Vector3.zero, rotation: Quaternion.identity, nodeSize: 1.0f);
// Scan the graph at the new position
AstarPath.active.Scan();
Move a graph to following an agent
If you have a big, or even infinite, procedural world, you may want to have a graph that is centered on the player, and follows the player as he/she moves around. This can technically be done by moving the graph and scanning it very often, but that is not very efficient. Instead, you may want to use the ProceduralGraphMover component, which can move a grid, layered grid or recast graph around at runtime much more efficiently by only recalculating the nodes that are actually affected by the movement.
Note that in contrast to the methods above, the ProceduralGraphMover will leave existing nodes at their original worlds positions, and instead only change the graph bounds to cover a different part of the world.