Interface ITraversalProvider
Provides additional traversal information to a path request.
The ITraversalProvider interface is something you can implement to be able to control exactly which nodes are traversable, how costly it is to traverse them, and how costly it is to traverse connections between nodes. This enables a lot of flexibility in how you can control pathfinding, and is useful for many different scenarios.
For example, you can use it to:
Make some agents prefer to walk on roads, while others prefer grass.
Create restricted doors, which only some agents can pass through.
In a turn based game, add additional cost for moving across a river between two tiles.
Etc.
Example implementation: public class MyCustomTraversalProvider : ITraversalProvider {
You can use it with a Seeker:
public bool CanTraverse (ref TraversalConstraint traversalConstraint, GraphNode node) {
// If, for example, your agents are afraid of heights, prevent your agents from going above 50 world units
return ((Vector3)node.position).y < 50;
}
public bool CanTraverse (ref TraversalConstraint traversalConstraint, GraphNode from, GraphNode to) {
// Connections between nodes can be filtered too.
// If you don't have any special rules for this, just forward the call to checking if the 'to' node is traversable.
// (or just skip this method, as the default implementation will do the same)
// We can, for example, prevent the agent from going directly from a node with tag 2 to a node with tag 3
if (from.Tag == 2 && to.Tag == 3) return false;
return CanTraverse(ref traversalConstraint, to);
}
public float GetTraversalCostMultiplier (ref TraversalCosts traversalCosts, GraphNode node) {
// For example, if your agent is afraid of heights, you can make nodes high up be 10 times more expensive to traverse
if (((Vector3)node.position).y > 30) return 10.0f;
return DefaultITraversalProvider.GetTraversalCostMultiplier(ref traversalCosts, node);
}
public uint GetConnectionCost (ref TraversalCosts traversalCosts, GraphNode from, GraphNode to) {
// The traversal cost is, by default, the sum of the penalty of the node's tag and the node's penalty
return traversalCosts.GetTagEntryCost(to.Tag) + to.Penalty;
// alternatively:
// return DefaultITraversalProvider.GetConnectionCost(ref traversalCosts, from, to);
}
}seeker.traversalProvider = new MyCustomTraversalProvider();
Or with a FollowerEntity followerEntity.pathfindingSettings.traversalProvider = new MyCustomTraversalProvider();
Or with a path instance: var path = ABPath.Construct(Vector3.zero, Vector3.one);
var traversalProvider = new MyCustomTraversalProvider();
// Use the same provider for both traversability and costs
path.traversalConstraint.traversalProvider = traversalProvider;
path.traversalCosts.traversalProvider = traversalProvider;
Your implementation of this interface may be called from separate threads if pathfinding multithreading is enabled, or if you are using the FollowerEntity component. You must ensure that your implementation is thread-safe. It is recommended to make each ITraversalProvider instance immutable, so that it can be shared between threads without any issues.
The ITraversalProvider may continue to be used by the movement script after the path has been calculated, in order to repair or simplify its path.
Public Methods
Multiplier for the cost of traversing some distance across the given node.
Public Variables
Filter diagonal connections using GridGraph.cutCorners for effects applied by this ITraversalProvider.