void FindPath(Vector3 startPosition, Vector3 targetPosition) { Node startNode = grid.NodeFromWorldPoint(startPosition); Node targetNode = grid.NodeFromWorldPoint(targetPosition); List <Node> openSet = new List <Node> (); HashSet <Node> closedSet = new HashSet <Node> (); openSet.Add(startNode); while (openSet.Count > 0) { Node currentNode = openSet [0]; for (int i = 1; i < openSet.Count; i++) { if (openSet [i].fCost < currentNode.fCost || openSet [i].fCost == currentNode.fCost && openSet [i].hCost < currentNode.hCost) { currentNode = openSet [i]; } } openSet.Remove(currentNode); closedSet.Add(currentNode); if (currentNode == targetNode) { RetracePath(startNode, targetNode); return; } foreach (Node neighbour in grid.GetNeighbours(currentNode)) { if (!neighbour.walkable || closedSet.Contains(neighbour)) { continue; } int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) { neighbour.gCost = newMovementCostToNeighbour; neighbour.hCost = GetDistance(neighbour, targetNode); neighbour.parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } }