public void SetWaypointManager(WaypointPathManager aControl) { MyWayControl = aControl; // 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; } }
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; } 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(); return; } if (CurrentWaypointTransform == null) { // grab our transform reference from the waypoint controller CurrentWaypointTransform = MyWayControl.GetWaypoint(CurrentWaypointNum); } // 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.y = 0; // get waypoint position and 'flatten' it _nodePosition = CurrentWaypointTransform.position; _nodePosition.y = 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; // 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; // 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 SetWaypointManager(WaypointPathManager 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; } }