示例#1
0
    void SetCharWaypoint(Vector3 position)
    {
        if (selectedChar.path.waypoints.Count < 1)
        {
            startNode = FindClosestNode(selectedChar.position);
        }
        else
        {
            startNode = FindClosestNode(selectedChar.path.waypoints[selectedChar.path.waypoints.Count - 1].position);
        }
        goalNode = FindClosestNode(position);

        if (goalNode != null && startNode != null && startNode != goalNode)
        {
            AStarMap astar   = new AStarMap(nodeList, goalNode, startNode);
            Path     newPath = astar.AStarPath();
            newPath.OptimizePath();

            selectedChar.AddWaypoints(newPath.waypoints);
            selectedChar.path.CreatePathMesh();
            PurgeAllNodes();
        }
        else
        {
            Debug.Log("start and goal node is probably the same");
        }

        startNode = null;
        goalNode  = null;
    }
示例#2
0
    void SetCharWaypoint(Vector3 position)
    {
        if (startNode == null)
        {
            startNode = FindClosestNode(selectedChar.position);
        }
        Node goalNode = FindClosestNode(position);

        if (goalNode != null)
        {
            AStarMap astar = new AStarMap(nodeList, goalNode, startNode);
            astar.AStarPath();

            Path path = new Path(astar.result);
            allPaths.Add(path);
            path.OptimizePath();
            selectedChar.path = path;
        }
        else
        {
            if (selectedChar.path.waypoints.Count >= 1)
            {
                for (int i = 0; i < selectedChar.path.waypoints.Count - 2; i++)
                {
                    Vector3 closestPoint = ClosestPointOnLine(selectedChar.path.waypoints[i].position, selectedChar.path.waypoints[i + 1].position, position);
                    if (Vector3.Distance(closestPoint, position) < 0.5f)
                    {
                        Node newNode = new Node(closestPoint.x, closestPoint.z);
                        nodeList.Add(newNode);
                        break;
                    }
                }
            }
        }

        startNode = null;
        goalNode  = null;
    }