A* Pathfinding Implementation

Hello guys.

I just want to share my A* Pathfinding Project Implementation so far.

The script is far from finished but could be a good starting point for someone trying to do things like pathfinding and finding ValidGridPosition via A* Pathfinding Project

public static class PathfindingUtils
    {
        private static GameObject _gridVisualParent;
        private static List<GameObject> _gridVisualObjects;

        public static List<GraphNode> GetValidGraphNodesInRange(Transform sourceTransform, int range, GameObject gridNodeVisual = null, bool displayPathNodes = true)
        {
            var path = ConstantPath.Construct(sourceTransform.position, (range *  2 +  1) * 1000);

            AstarPath.StartPath(path);

            path.BlockUntilCalculated();

            if (displayPathNodes && gridNodeVisual != null)
            {
                DisplayAvailableGraphNodes(path, gridNodeVisual);
            }
            return path.allNodes;
        }
        
        private static void DisplayAvailableGraphNodes(ConstantPath path, GameObject gridNodeVisual)
        {
            if (_gridVisualParent == null)
            {
                _gridVisualParent = new GameObject("Grid VisualObjects Parent");
            }
            
            _gridVisualParent.transform.DestroyAllChildTransforms();
            _gridVisualObjects = new List<GameObject>();
            
            foreach (var node in path.allNodes) {
                if (node != path.startNode) {
                    GameObject go = GameObject.Instantiate(gridNodeVisual, (Vector3)node.position, Quaternion.identity) as GameObject;
                    go.transform.SetParent(_gridVisualParent.transform);
                    _gridVisualObjects.Add(go);
                }
            }
        }

        public static bool IsValidGridPosition(BaseUnit unit, BaseAction action, Vector3 position)
        {
            foreach (GraphNode graphNode in GetValidGraphNodesInRange(unit.transform, action.Range))
            {
                if ((Vector3)graphNode.position == position)
                {
                    return true;
                }
            }
            return false;
        }
    }

GetValidGraphNodesInRange

This method returns a list of GraphNodes (more or less equivalent to the GridPosition) that are in a specific range. Obstacles are already ignored if the pathfinding component of A* Project is configured correctly

DisplayAvailableGraphNodes

This method displays the pathfinding nodes found. For this purpose, a GameObject is passed (similar to the procedure with GridDebugObjects)

IsValidGridPosition

This method just check if a worldPosition is valid. This is actually just for testing and will be transfered to the specific Action.

I hope the script helps a few people get started with A*
Pathfinding Project

That’s a good start! It’s definitely best to think in terms of nodes and neighbors rather than specific forms… for example, if we leave the gathering of neighbors and the cost to move from node to node up to the nodes themselves, then the same Pathfinding can be used for Hex, multi-floor, standard tile, or any other node style virtually without modification.

Privacy & Terms