private 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 = 0; 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 (closedSet.Contains(neighbour)) { continue; } switch (neighbour.NodeType) { case NodeType.Walkable: break; case NodeType.NotWalkable: continue; case NodeType.Climable: break; default: break; } int newMovementCostToNeighbor = currentNode.GCost + GetDistance(currentNode, neighbour); if (newMovementCostToNeighbor < neighbour.GCost || !openSet.Contains(neighbour)) { neighbour.GCost = newMovementCostToNeighbor; neighbour.HCost = GetDistance(neighbour, targetNode); neighbour.Parent = currentNode; if (!openSet.Contains(neighbour)) { openSet.Add(neighbour); } } } } }