Learn to code by Making 2.5 D Space Shooter Game.

Suleiman Abdullah
3 min readDec 21, 2021

Objective : Understand coroutine by Implement Triple Shot cool down system.

We do these using coroutine , this method will allow us to suspend its execution and return control to unity but then continue where it left off on the following frame.

This Method is type ienumerator ,which allow to pause execution until the given yield instruction finishes then continue where it left off the following frame.

The first step we need to create TripleShotPowerRoutine coroutine inside Player scripts.

This is error is because coroutine require yield keyword to (pause execution) , return keyword and time to pause to return control where it left in the following frame .

Null means it will pause execution every one frame.

We need to allow our ship to take some time to destroy its enemy using triple shot after collect Triple Shot powerup , In order to be able to define our time we use new keyword to able to define our custom time WaitForSeconds(“Time in seconds”) is class which enable us to pass time we want to wait as float.

When using new keyword we are instantiate WaitForSeconds class if we do this for the life of our game you need to understand this is a reference variable which means it create memory allocation every time we run this.

The best practise using coroutine is to cache reference variable of WaitForSeconds .

Since we don't want to fire our triple shot every 5 seconds but forever and always like that song , after yield instruction finishes we want to turn off our triple shot until we collect again Triple shot powerup .

Lets call our coroutine when we activate our triple shot and see the result.

It doesn't work we are firing triple shot forever!!!! coroutine is not called like traditional method.

In order to call coroutine we use Unity keyword “StartCoroutine();” ,then inside of StartCoroutine call your coroutine method.

One stone two bird, now you will have strong understand of coroutine and how to implement cool down of any type using coroutine.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development