Struct TraversalCosts

Public

Defines the cost of traversing specific nodes when pathfinding.

To make agents avoid certain nodes, you can set a high penalty for traversing them. This will make the pathfinding algorithm prefer other nodes instead.

You can set costs based on the node's tag, or you can use a custom traversal provider to set costs based on other criteria with completely custom logic.

var traversalCosts = new TraversalCosts();

// Make tags 1 and 4 have a higher penalty
var tagCostMultipliers = new float[32];
tagCostMultipliers[1] = 2.5f;
tagCostMultipliers[4] = 5.0f;
traversalCosts.tagCostMultipliers = tagCostMultipliers;

var path = ABPath.Construct(Vector3.zero, Vector3.one);
path.traversalCosts = traversalCosts;
Traversal cost calculation Assume we have a path with 3 nodes: A, B, and C. The path spends 1 meters inside node A, 3 meters inside node B, and 2 meters inside node C.

The total cost of traversing the path is:

var totalCost = 0f;
totalCost += costs.GetTraversalCostMultiplier(A) * 1000; // 1 meter in A
totalCost += costs.GetConnectionCost(A, B); // cost of moving from A to B
totalCost += costs.GetTraversalCostMultiplier(B) * 3000; // 3 meters in B
totalCost += costs.GetConnectionCost(B, C); // cost of moving from B to C
totalCost += costs.GetTraversalCostMultiplier(C) * 2000; // 2 meters in C
or, assuming no traversal provider is set:

var totalCost = 0f;
totalCost += costs.tagCostMultipliers[A.Tag] * 1000; // 1 meter in A
totalCost += costs.tagEntryCosts[B.Tag] + B.Penalty; // cost of moving from A to B
totalCost += costs.tagCostMultipliers[B.Tag] * 3000; // 3 meters in B
totalCost += costs.tagEntryCosts[C.Tag] + C.Penalty; // cost of moving from B to C
totalCost += costs.tagCostMultipliers[C.Tag] * 2000; // 2 meters in C

Note

On navmesh/recast graphs, the distance the path moves inside each node (triangle) is approximated. It cannot be exactly calculated for performance reasons. Typically it averages out to around 10-20% higher than the actual distance. This is usually not a problem in practice, but you should keep this in mind and not rely on exact values.

Public Methods

GetConnectionCost (from, to)

Instantaneous cost for moving between from and to.

Cost for traversing the given node, ignoring any traversalProvider that may be set.

Like GetTraversalCostMultiplier, but ignores any traversalProvider that may be set.

Cost for entering a node with the given tag.

Cost for traversing the given node.

Multiplier for the cost of traversing some distance across the given node.

Public Variables

hasCosts

True if traversal costs might be non-default, and false if they are guaranteed to be default for all nodes.

Public
tagCostMultipliers

Multiplier for the cost of moving some distance across a node with a given tag.

Public
tagCostMultipliersInternal
Public
tagEntryCosts

How much it costs to enter a node with a given tag.

Public
tagEntryCostsInternal
Public
traversalProvider

Traversal provider to forward all cost calculations to.

Public