Editing graph connections manually

How to make edits to the graph structure.

Sometimes the graph is almost right, but there may be some cases where you need it fixed up a bit. This is most common with the point graph and this page is mostly intended for that graph type. The methods do work for other graph types as well to some extent, however.

See

Off-mesh links if you want to handle jumping, climbing, teleporters and other special movement logic.

If you have a point graph where you want to either add a connection, remove an existing connection or perhaps change the cost of some connection, then you can use the NodeLink component. Point graphs are generally created from a collection of GameObjects. You can add the NodeLink component to one node and then set the target to another node. By changing the options, the component can then either add a new connection between those two nodes, remove an already existing connection (if one exists) or change the cost of the connection between them. These new connections are identical to the ones created by the point graph originally.

To make it easier to modify the graph, there are some keyboard shortcuts. You can find the menu items in Unity under menubar -> Edit -> Pathfinding.

  • alt+ctrl+L (alt+cmd+L on macOS): Link two nodes. Connect the nodes if there is no NodeLink connection between them. If they are already connected, then explicitly delete their connection.

  • alt+ctrl+U (alt+cmd+U on macOS): Unlink two nodes. This removes the NodeLink components that link the two nodes, regardless of whether the NodeLink is configured to add or delete a connection.

  • alt+ctrl+B (alt+cmd+B on macOS): Destroy all NodeLink components on the selected GameObjects.

Editing graph connections using code

You can also modify graph connections using code.

// Connect two nodes
var node1 = AstarPath.active.GetNearest(transform.position, NNConstraint.None).node;
var node2 = AstarPath.active.GetNearest(transform.position + Vector3.right, NNConstraint.None).node;
var cost = (uint)(node2.position - node1.position).costMagnitude;

GraphNode.Connect(node1, node2, cost, OffMeshLinks.Directionality.TwoWay);