public void FindWaypoints() { waypoints.Clear(); GameObject[] foundWaypoints = GameObject.FindGameObjectsWithTag("GuardWaypoint"); foreach (var waypoint in foundWaypoints) { // A waypoint may contain more than one guard script to minimsie the amoutn of objects, // it is necessary to iterate through them and find the right one. GuardWaypoint[] scripts = waypoint.GetComponents <GuardWaypoint>(); foreach (GuardWaypoint script in scripts) { // Checking if the script ref number is the same as the gaurd ref number. if (script.routeNumber == guardNumber && script.waypointNumber == 0) { // If it is, and it is also waypoint 0, add it to the list of this guard's waypoints // and make it the first waypoint the guard will go to to start pratolling. waypoints.Add(script); currentWaypoint = script; } // Otherwise if the ref matches but it is not 0, add it to the list of waypoints. else if (script.routeNumber == guardNumber) { waypoints.Add(script); } } } }
// What to execute before departure. void NextWaypoint() { // Shift route. if (currentWaypoint.type == GuardWaypoint.waypointType.shift) { var altWaypoints = new List <GuardWaypoint>(); foreach (var waypoint in waypoints) { if (waypoint.waypointNumber == nextWaypointNumber) { altWaypoints.Add(waypoint); } } currentWaypoint = altWaypoints[Random.Range(0, altWaypoints.Count)]; routeAltNumber = currentWaypoint.routeAltNumber; navMeshAgent.destination = currentWaypoint.transform.position; return; } // Skip to... if (currentWaypoint.type == GuardWaypoint.waypointType.skipTo) { currentWaypoint = currentWaypoint.skipRef; nextWaypointNumber = currentWaypoint.waypointNumber + 1; routeAltNumber = currentWaypoint.routeAltNumber; navMeshAgent.destination = currentWaypoint.transform.position; return; } // Normal procedure. foreach (var waypoint in waypoints) { if (waypoint.waypointNumber == nextWaypointNumber) { if (waypoint.routeAltNumber == routeAltNumber) { if (debug) { Debug.Log(name + " set " + waypoint.type); } currentWaypoint = waypoint; navMeshAgent.destination = currentWaypoint.transform.position; return; } } } // Failure, skip and retry. Debug.LogError("Missing waypoint on " + guardNumber); nextWaypointNumber = 0; NextWaypoint(); }