Using WaitUntil to Delay a Coroutine in Unity3D

--

Objective: Delay the Enemy’s ability to fire a laser until the Enemy GameObject is within the screen boundaries.

Step 1: In the Enemy script, create a private bool called _canFire and set it to false.

Step 2: Create a private method called CanFireLaser() and create an if statement stating that if the Enemy GameObject’s X position is greater than -10 and less than 10, and the Y position is less than 6.5, then _canFire is set to true.

Step 3: Within the Update() method, call the CanFireLaser() method.

Step 4: Within the InstantiateEnemyLaserRoutine() coroutine, use the WaitUntil function to delay the laser instantiation until _canFire is equal to true.

Now the Enemy will not be able to fire a laser until it is visible on-screen.

--

--