Beispiel #1
0
    void PathCreation()
    {
        //create the waypointIndex array to hold to randomly selected waypoints along the track
        waypointIndex = new int[Waypoint_Cache.waypoints.Count];

        //print(Waypoint_Cache.waypoints.Count);
        //print(Waypoint_Cache.waypoints[0].transform.position.x);
        //print(Waypoint_Cache.waypoints[Waypoint_Cache.waypoints.Count-1].transform.position.x);

        //randomly assign the selected waypoints at each area
        for (int i = 0; i < Waypoint_Cache.waypoints.Count; i++)
        {
            waypointIndex[i] = waypointChildInt;//Random.Range(0, 5);
        }
        //print(waypointIndex.Length);

        //create and assign the starting point and goal point for the first waypoint
        Transform startWaypoint = transform;
        Transform goalWaypoint  = Waypoint_Cache.waypoints[0].transform.GetChild(waypointIndex[0]);

        //special case for the first waypoint starting from the ai car's location
        bestPath = new Stack <Node>(new Stack <Node>(aStar.CalulatePath(startWaypoint.position.x, startWaypoint.position.z, goalWaypoint.position.x, goalWaypoint.position.z)));

        //pop off the first waypoint, to ensure no duplicates in the fullPath list
        bestPath.Pop();

        //pop off all node locations and add to the full path list to follow from start to finish
        while (bestPath.Count > 0)
        {
            fullPath.Add(bestPath.Pop());
        }

        //for all remaining track waypoint segments
        for (int i = 1; i < Waypoint_Cache.waypoints.Count; i++)
        {
            //assign starting waypoint location of segment and goal waypoint location of segment
            startWaypoint = Waypoint_Cache.waypoints[i - 1].transform.GetChild(waypointIndex[i - 1]);
            goalWaypoint  = Waypoint_Cache.waypoints[i].transform.GetChild(waypointIndex[i]);

            //generate the path for the segment
            bestPath = new Stack <Node>(new Stack <Node>(aStar.CalulatePath(startWaypoint.position.x, startWaypoint.position.z, goalWaypoint.position.x, goalWaypoint.position.z)));

            //pop off the first waypoint, to ensure no duplicate waypoints in the fullPath list
            bestPath.Pop();

            //print(i + " Path");

            //pop off all node locations and add to the full path list to follow from start to finish
            while (bestPath.Count > 0)
            {
                fullPath.Add(bestPath.Pop());
            }
        }

        //GameManager.raceIsStarting = false;
    }