Struct TraversalConstraint
Specifies which nodes are traversable and which are not.
This struct is used to specify which nodes are traversable when searching for paths and when doing linecasts on graphs, among other things.
It can be used to filter out nodes based on their tags, or even using completely custom filters.
For performance reasons, this struct is often passed by reference (using the ref keyword). It's a moderately large struct, and it is often used in performance-critical code. Passing it by reference avoids copying the struct, and gives us some moderate performance improvements.
// Custom filter
var traversalConstraint = new TraversalConstraint((GraphNode node) => ((Vector3)node.position).y > 50);
traversalConstraint.tags = 1 << 3; // Only allow traversing nodes with tag 3
// Convert to nearest node constraint
var nearestNodeConstraint = traversalConstraint.ToNearestNodeConstraint();
var node = AstarPath.active.GetNearest(transform.position, nearestNodeConstraint).node;
// Check if the node is traversable
traversalConstraint.CanTraverse(node); // Truevar graph = AstarPath.active.data.recastGraph;
var start = transform.position;
var end = start + Vector3.forward * 10;
var trace = new List<GraphNode>();
var traversalConstraint = TraversalConstraint.None;
traversalConstraint.tags = 1 << 3; // Only allow traversing nodes with tag 3
if (graph.Linecast(start, end, out GraphHitInfo hit, ref traversalConstraint, trace)) {
Debug.Log("Linecast traversed " + trace.Count + " nodes before hitting an obstacle");
Debug.DrawLine(start, hit.point, Color.red);
Debug.DrawLine(hit.point, end, Color.blue);
} else {
Debug.Log("Linecast traversed " + trace.Count + " nodes");
Debug.DrawLine(start, end, Color.green);
}ABPath path = ABPath.Construct(currentPosition, destination, null);
path.traversalConstraint.traversalProvider = GridShapeTraversalProvider.SquareShape(3);
// If you are writing your own movement script
seeker.StartPath(path);
// If you are using an existing movement script (you may also want to set ai.canSearch to false)
// ai.SetPath(path);
Public Methods
True if the node is traversable with respect to the constraints set in this struct, but without checking the filter or the traversalProvider.
A nearest node constraint which uses the same settings as this traversal constraint.
Public Variables
Allow traversing only nodes which the given filter function returns true for.
Filter diagonal connections on grid graphs using GridGraph.cutCorners for effects applied by any assigned ITraversalProvider.
Graphs which are traversable.
Set of tags which are traversable.
Allow traversing only nodes that are also traversable by the given ITraversalProvider.
Public Static Variables
Traversal constraint which allows all (walkable) nodes on all graphs to be traversed.