Turning the PowerUp Off

Since in this game, the Player should not have the Triple Shot PowerUp indefinitely, we will use a Coroutine to turn off or power down the Triple Shot effect after 5 seconds.

First, in the Player script, add a private float for the PowerUp duration with a value of 5. That will control how long the PowerUp lasts.

Float for PowerUp Duration

Then create a new Coroutine named “TripleShotPowerDownRoutine”, have it yield for _powerUpDuration, and turn _isTripleShotActive to false after the yield. That will let the Player have the Triple Shot effect last for 5 seconds. Start the Coroutine in TripleShotActive after setting _isTripleShotActive to true.

Power Down Routine

The Power Up now lasts for 5 seconds and then turns off. The only issue is if the Player picks up another Triple Shot while Triple Shot is active, the timer does not extend, as you can see from the timestamps in the Console. It only lasts the set 5 seconds.

Triple Shot Powering Down

To have the timer extend if the Player picks up another Triple Shot while it is active, the way the Coroutine is yielding needs to be changed.

Timestamps

In the Player script, in the TripleShotPowerDownRoutine to increase the time, create a while loop that will run as long as the _powerUpDuration is greater than 0 and have it yield for 1 second and then decrease _powerUpDuration by 1. After the while loop has been completed, reset the _powerUpDuration back to 5. Then in the TripleShotActivate method, add an else statement so that if Triple Shot is active, you increase _powerUpDuration by 5. That will extend how long the Coroutine will run.

Extended Coroutine

The extended time is now working, as you can see in the Console timestamps.

Timestamps

Now that the first PowerUp is working, we have a base to work off and make modular, which we will see in the following article.

Unity Developer, Software Engineer, Game Designer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store