public List <PathNode> FindPath(int startX, int startY, int endX, int endY) { PathNode startNode = pathGrid.GetGridObject(startX, startY); PathNode endNode = pathGrid.GetGridObject(endX, endY); openList = new List <PathNode> { startNode }; closedList = new List <PathNode>(); for (int x = 0; x < pathGrid.GetWidth(); x++) { for (int y = 0; y < pathGrid.GetHeight(); y++) { PathNode pathNode = pathGrid.GetGridObject(x, y); pathNode.gCost = int.MaxValue; pathNode.CalculateFCost(); pathNode.cameFromNode = null; } } startNode.gCost = 0; startNode.hCost = CalculateDistanceCost(startNode, endNode); startNode.CalculateFCost(); while (openList.Count > 0) { PathNode currentNode = GetLowestFCost(openList); if (currentNode == endNode) { //End reached return(CalculatePath(endNode)); } openList.Remove(currentNode); //current node already searched closedList.Add(currentNode); foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) { if (closedList.Contains(neighbourNode)) { continue; } int tentativeGCost = currentNode.gCost + CalculateDistanceCost(currentNode, neighbourNode); if (tentativeGCost < neighbourNode.gCost) { Debug.DrawLine(pathGrid.GetTilePosition(currentNode.x, currentNode.y) + 0.45f * Vector3.one, pathGrid.GetTilePosition(neighbourNode.x, neighbourNode.y) + 0.45f * Vector3.one, Color.yellow, 200f, false); neighbourNode.cameFromNode = currentNode; neighbourNode.gCost = tentativeGCost; neighbourNode.hCost = CalculateDistanceCost(neighbourNode, endNode); neighbourNode.CalculateFCost(); if (!openList.Contains(neighbourNode)) { openList.Add(neighbourNode); // PROBLEM IF MULTIPLE SOLUTIONS??? } } } } // out of node in the open list return(null); }