void Patrolling() { // Set an appropriate speed for the NavMeshAgent. nav.isStopped = false; nav.speed = patrolSpeed; // If near the next waypoint or there is no destination... if (nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance) { // ... increment the timer. patrolTimer += Time.deltaTime; // If the timer exceeds the wait time... if (patrolTimer >= patrolWaitTime) { if (currentWaypoint == null) { currentWaypoint = GetClosestWaypoint(); if (upperPatrolWaypoints.Contains(currentWaypoint)) { currentWaypoints = upperPatrolWaypoints; } else { currentWaypoints = lowerPatrolWaypoints; } } else { lastWaypoint = currentWaypoint; GameObject[] linkedWaypoints = currentWaypoint.GetComponent <DoneWayPointGizmo>().linkedWaypoints; currentWaypoint = linkedWaypoints[Random.Range(0, linkedWaypoints.Length)]; while (!currentWaypoints.Contains(currentWaypoint)) { currentWaypoint = linkedWaypoints[Random.Range(0, linkedWaypoints.Length)]; } } // Reset the timer. patrolTimer = 0; } } else { // If not near a destination, reset the timer. patrolTimer = 0; } // Set the destination to the patrolWayPoint. if (currentWaypoint != null) { nav.destination = currentWaypoint.transform.position; } if (currentWaypoints != null) { GameObject waypointClosestToPlayer = lastPlayerSighting.WaypointClosestToPlayer(); if (!currentWaypoints.Contains(waypointClosestToPlayer) && !transitionWaypoints.Contains(waypointClosestToPlayer)) { doTransition = true; lastWaypoint = currentWaypoint; currentWaypoints = transitionWaypoints; if (upperPatrolWaypoints.Contains(lastWaypoint)) { currentWaypoint = startTransitionToLower; transitioningTo = lowerPatrolWaypoints; } else { currentWaypoint = startTransitionToUpper; transitioningTo = upperPatrolWaypoints; } return; } } }