How to use Path's Travel Constraint Filter Correctly

  • A* version: [5.46]

  • Unity version: [6000.0.59f2]

I’m a bit confused the relationship between a path that was calculated and the path.traversalConstraint.filter being applied to said path.

My goal is to make a FollowerEntity pursue a path, and then add the path’s nodes to a filter so that other Entities will choose different paths/nodes.


public FollowerEntity ai;
public Transform target;

//Start Function

var blockedNodes = new HashSet();
ABPath path = ABPath.Construct(transform.position, target.position);

path.traversalConstraint.filter = (GraphNode node) => !blockedNodes.Contains(node);

AstarPath.StartPath(path, true);

path.BlockUntilCalculated();

ai.SetPath(path);

foreach (var node in path.path)
{
    blockedNodes.Add(node);
}


When the games runs the Follower starts to follow the path but then stops shortly.

The FollowerEntity behaves normally (it moves to the target) when I comment out the last foreach loop that adds the blocked nodes.

If the path has been calculated then why is the filter that was set before being applied later?

Hi

The traversal constraint is used not only when the agent is searching for the path, but also when it follows it. In your case, you are immediately telling the agent that it cannot follow the path it just calculated.

Make sure you use different traversal constraints for different agents, so that they know which nodes to avoid.

Btw. I would recommend using penalties instead of blocking the nodes completely, as otherwise agents may think they are not able to reach their goal at all.