//Finds the closest path to PacMan from the ghost position.

    void FindPath(Vector3 StartPos, Vector3 targetPos)
    {
        node StartNode  = grid.PacmanPosition(StartPos);
        node targetNode = grid.PacmanPosition(targetPos);

        List <node>    openSet   = new List <node>();
        HashSet <node> closetSet = 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);
            closetSet.Add(currentNode);

            if (currentNode == targetNode)
            {
                RetracePath(StartNode, targetNode);
                return;
            }

            foreach (node neighbor in grid.GetNeighbors(currentNode))
            {
                if (!neighbor.canWalk || closetSet.Contains(neighbor))
                {
                    continue;
                }

                int newMovementCost = currentNode.gCost + GetDistance(currentNode, neighbor);

                if (newMovementCost < neighbor.gCost || !openSet.Contains(neighbor))
                {
                    neighbor.gCost = newMovementCost;
                    neighbor.hCost = GetDistance(neighbor, targetNode);

                    neighbor.parent = currentNode;

                    if (!openSet.Contains(neighbor))
                    {
                        openSet.Add(neighbor);
                    }
                }
            }
        }
    }