How can I improve my A* pathfinding function to consider tags and penalties

  • A* version: 5.0.6
  • Unity version: 2022.3.49f1

I’m working on a custom pathfinding function in Unity using the A* Pathfinding Project. My current implementation calculates a path between a start and end position, but it’s not considering tags or penalties as intended. It’s does calculate the path. Here’s my current code:

public void CalculatePath(Vector3 start, Vector3 dest, OnPathDelegate onPathCompleteDistance, AIGraphMask aIGraphMask, bool blockUntilCalculated = false, bool pushToFront = false)
{
    NNInfo nearestNodeInfo = AstarPath.active.GetNearest(dest);

    // Check if the node is walkable
    if (nearestNodeInfo.node != null && nearestNodeInfo.node.Walkable)
    {
        var path = ABPath.Construct(start, dest, onPathCompleteDistance);

        // Set the graph mask if specified
        path.nnConstraint.graphMask = AStarPathfinding.GetGraphMask(aIGraphMask);
        path.nnConstraint.tags = (1 << 0);

        path.nnConstraint.constrainWalkability = true;
        path.nnConstraint.constrainArea = true;
        path.nnConstraint.constrainTags = true;
        path.nnConstraint.constrainDistance = true;

        AstarPath.StartPath(path, pushToFront);

        if (blockUntilCalculated)
        {
            path.BlockUntilCalculated();
        }
    }
}

I have a few specific questions:

  1. Why isn’t my function considering tags even though I’ve set path.nnConstraint.tags = (1 << 0) and path.nnConstraint.constrainTags = true?
  2. How can I modify this function to take penalties into account when calculating the path?
  3. Are there any other improvements or best practices I should consider for this pathfinding implementation?

Any help or guidance would be greatly appreciated. Thanks!

Hi

For historical reasons, the nearest node constraint and the path settings are distinct.
You can set:

path.enabledTags = ...;
path.tagPenalties = ...;

Thank you for the previous help. I’ve managed to get the tags working correctly in my pathfinding function, but I’m still having issues with penalties. Here’s my current situation:

  1. I’m setting penalties for specific nodes using this code:

csharp

var pathNode = AstarPath.active.GetNearest(node.worldPosition, nn).node;
pathNode.Penalty = 6000;
  1. I’m not using tagPenalties, but rather setting penalties directly on nodes.
  2. When I calculate the path, I want it to consider these node-specific penalties that I’ve set.

Hi

It should consider these penalties by default.