Determining How Long Powerup Effects Should Last

--

Setting respawn rates for objects that are spawned using IEnumerator coroutines is easy using ‘yield return’ combined with the WaitForSeconds function.

The above example shows two uses of IEnumerator. Both utilize infinate loops predicated on the _stopSpawning variable and both use ‘yield return’ to delay repetition of the loops.

These coroutines can run simultaneously as though they are their own little programs; busily executing their charge, delayed only by the WaitForSeconds function that forces them to pause execution until the appropriate amount of time has passed.

Notice that the IEnumerators employ the Random.Range function to introduce an element of the unknown into our game. Not being able to predict the appearance location or the eta of an Enemy or PowerUp adds some excitement to our game!

--

--

Richard Morgan
Richard Morgan

Written by Richard Morgan

Unity developer interested in AI integrations.

No responses yet