Function GraphNode.Connect
Adds a connection between two nodes.
void Connect (
lhs | First node to connect. |
|
rhs | Second node to connect |
|
uint | cost | Cost of the connection. A cost of 1000 corresponds approximately to the cost of moving one world unit. See Int3.Precision. |
directionality=OffMeshLinks.Directionality.TwoWay | Determines if both lhs->rhs and rhs->lhs connections will be created, or if only a connection from lhs->rhs should be created. |
Adds a connection between two nodes.
If the nodes already have a connection to each other, that connection will be updated with the new cost.
Note that some graphs have a special representation for some connections which is more efficient. For example grid graphs can represent connections to its 8 neighbours more efficiently. But to use that efficient representation you'll need to call GridNode.SetConnectionInternal instead of this method.
This is different from an off-mesh link. An off-mesh link contains more metadata about the connection and is in many cases preferable to use instead of this method. This is a much lower-level method which is used by off-mesh links internally.
Movement scripts such as the RichAI or FollowerEntity may get confused if they try to follow a connection made using this method as it does not contain any information about how to traverse the connection.
Internally, both nodes keep track of the connection to the other node, even for a one-way connection. This is done to make sure the connection can always be removed later on, if for example one of the nodes is destroyed.
// 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);
AddPartialConnection which is a lower level method. But if you use it, you need to handle invariants yourself.