Tier 4
Series 300
300B: Time-Dilation
"Imagine a pilot staring at their watch every second for three hours to know when it's time to land. They can't focus on the horizon because they're obsessed with the clock. A Coroutine is an alarm clock; the pilot goes to sleep or focuses elsewhere, and the system wakes them up exactly when the timer hits zero."
The Concept: IEnumerators & Yields
As explored in Chapter 10, Coroutines allow you to spread tasks over multiple frames. Instead of "checking" for a condition, the code "waits" for it.
Yielding: The code pauses here and returns control to Unity until the next frame or a set time.
Linear Logic: Complex sequences (Fade Out -> Wait 2s -> Load Level) are written in order, not scattered across if-statements.
Yielding: The code pauses here and returns control to Unity until the next frame or a set time.
Linear Logic: Complex sequences (Fade Out -> Wait 2s -> Load Level) are written in order, not scattered across if-statements.
Red Flag Detected
The AI Trap: "The Update Watcher"
You ask the AI: "Make the enemy wait 3 seconds before shooting again."
// AI-Generated Code: Audit Failure (Update Clutter)
float timer = 0f;
void Update() {
// Audit Fail: This runs 60+ times a second just to check a clock!
timer += Time.deltaTime;
if (timer >= 3f) {
Shoot();
timer = 0f;
}
}
While functional, if you have 100 enemies, you have 100 unnecessary "clock checks" every frame. It makes the code harder to read and drains performance.
Elite Telemetry
Research shows "Elite" teams achieve 15% faster lead times by keeping AI on a "very tight leash."
- Small Batches Solving one problem at a time prevents logic drift.
- Modular Design Localizing the "blast radius" of AI changes.
- Tight Loops Rapid iteration with constant code review.
The Navigator's Command
Corrective Protocol
// Navigator Code: Clean & Event-Based
void Start() {
StartCoroutine(ShootingRoutine());
}
IEnumerator ShootingRoutine() {
while (true) {
Shoot();
// The Navigator "sleeps" for 3 seconds here
yield return new WaitForSeconds(3f);
}
}
Your Pilot Command
> A skilled Navigator directs the AI to use an IEnumerator.