public void SetWayController(Waypoints_Controller aControl) { myWayControl = aControl; aControl = null; // grab total waypoints totalWaypoints = myWayControl.GetTotal(); // make sure that if you use SetReversePath to set shouldReversePathFollowing that you // call SetReversePath for the first time BEFORE SetWayController, otherwise it won't set the first waypoint correctly if (shouldReversePathFollowing) { currentWaypointNum = totalWaypoints - 1; } else { currentWaypointNum = 0; } Init(); // get the first waypoint from the waypoint controller currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum); if (startAtFirstWaypoint) { // position at the currentWaypointTransform position myTransform.position = currentWaypointTransform.position; } }
public void SetWayController(Waypoints_Controller aControl) { if (aControl == null) { return; } myWayControl = aControl; // grab total waypoints totalWaypoints = myWayControl.GetTotal(); if (shouldReversePathFollowing) { currentWaypointNum = totalWaypoints - 1; } else { currentWaypointNum = 0; } if (myWayControl.Closed) { loopPath = true; } else { loopPath = false; } Init(); // get the first waypoint from the waypoint controller currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum); if (startAtFirstWaypoint) { // position at the currentWaypointTransform position myTransform.position = currentWaypointTransform.position; } }
private void UpdateWaypoints() { // If we don't have a waypoint controller, we safely drop out if (myWayControl == null) { return; } if (reachedLastWaypoint && destroyAtEndOfWaypoints) { // destroy myself(!) Destroy(gameObject); return; } else if (reachedLastWaypoint) { currentWaypointNum = 0; reachedLastWaypoint = false; } // because of the order that scripts run and are initialised, it is possible for this function // to be called before we have actually finished running the waypoints initialization, which // means we need to drop out to avoid doing anything silly or before it breaks the game. if (totalWaypoints == 0) { // grab total waypoints totalWaypoints = myWayControl.GetTotal(); } if (currentWaypointTransform == null) { // grab our transform reference from the waypoint controller currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum); return; } // now we check to see if we are close enough to the current waypoint // to advance on to the next one myPosition = myTransform.position; myPosition.z = 0; // get waypoint position and 'flatten' it nodePosition = currentWaypointTransform.position; nodePosition.z = 0; // check distance from this to the waypoint currentWayDist = Vector3.Distance(nodePosition, myPosition); if (currentWayDist < waypointDistance) { // we are close to the current node, so let's move on to the next one! if (shouldReversePathFollowing) { currentWaypointNum--; // now check to see if we have been all the way around if (currentWaypointNum < 0) { // just incase it gets referenced before we are destroyed, let's keep it to a safe index number currentWaypointNum = 0; // completed the route! reachedLastWaypoint = true; // if we are set to loop, reset the currentWaypointNum to 0 if (loopPath) { currentWaypointNum = totalWaypoints - 1; // grab our transform reference from the waypoint controller currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum); // the route keeps going in a loop, so we don't want reachedLastWaypoint to ever become true reachedLastWaypoint = false; } // drop out of this function before we grab another waypoint into currentWaypointTransform, as // we don't need one and the index may be invalid return; } } else { currentWaypointNum++; // now check to see if we have been all the way around if (currentWaypointNum >= totalWaypoints) { // completed the route! reachedLastWaypoint = true; // if we are set to loop, reset the currentWaypointNum to 0 if (loopPath) { currentWaypointNum = 0; // grab our transform reference from the waypoint controller currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum); // the route keeps going in a loop, so we don't want reachedLastWaypoint to ever become true reachedLastWaypoint = false; } // drop out of this function before we grab another waypoint into currentWaypointTransform, as // we don't need one and the index may be invalid return; } } // grab our transform reference from the waypoint controller currentWaypointTransform = myWayControl.GetWaypoint(currentWaypointNum); } }
public void UpdateWaypoints() { if (!doneInit) { Init(); } // quick check to make sure that we have a reference to the waypoint manager if (waypointManager == null) { return; } // because of the order that scripts run and are initialised, it is possible for this function // to be called before we have actually finished running the waypoints initialization, which // means we need to drop out to avoid doing anything silly or before it breaks the game. if (totalWaypoints == 0) { // grab total waypoints totalWaypoints = waypointManager.GetTotal(); return; } // here, we deal with making sure that we always have a waypoint set up and // if not take the steps to find out what our current waypoint should be if (currentWaypointTransform == null) { currentWaypointNum = 0; currentWaypointTransform = waypointManager.GetWaypoint(currentWaypointNum); } // now we need to check to see if we are close enough to the current waypoint // to advance on to the next one myPosition = myTransform.position; myPosition.y = 0; // get waypoint position and 'flatten' it nodePosition = currentWaypointTransform.position; nodePosition.y = 0; // check distance from this car to the waypoint currentWaypointDist = Vector3.Distance(nodePosition, myPosition); if (currentWaypointDist < waypointDistance) { // we are close to the current node, so let's move on to the next one! currentWaypointNum++; // now check to see if we have been all the way around the track and need to start again if (currentWaypointNum >= totalWaypoints) { // completed a lap! set the lapDone flag to true, which will be checked when we go over // the first waypoint (so that you can't almost complete a race then go back around the // other way to confuse it) isLapDone = true; // reset our current waypoint to the first one again currentWaypointNum = 0; } // grab our transform reference from the waypoint controller currentWaypointTransform = waypointManager.GetWaypoint(currentWaypointNum); } // position our debug box at the current waypoint so we can see it (uncomment if you're debugging!) // debugBox.transform.position=currentWaypointTransform.position; }