Function ITraversalProvider.GetTraversalCostMultiplier

GetTraversalCostMultiplier (refTraversalCosts traversalCosts, GraphNode node)

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

Public
float GetTraversalCostMultiplier (

refTraversalCosts

traversalCosts

GraphNode

node

)

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

Returns a float representing how costly it is to move across the node, with 1.0 being the default, and, for example, 2.0 being twice as costly.

The value will be multiplied by the traversed distance in millimeters (see Int3.Precision) to get the final cost. For the default multiplier of 1.0, this means that the cost will be the same as the distance in millimeters.

Note

Prefer to make traversal more costly (cost multiplier >1), rather than making it cheaper than the default (cost multiplier <1). If you return any cost multiplier less than 1, you'll also need to reduce AstarPath.heuristicScale to the lowest cost multiplier that you use in the project, otherwise the pathfinding algorithm may not find the optimal path. Alternatively you could compensate with a higher connection cost value, so that the agent can never move to the target with a lower cost than the default. See https://en.wikipedia.org/wiki/Admissible_heuristic.

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);
}