Day 18 | Determining How Long Powerup Effects Should Last

Powerups are fun to make, but having permanent powerups might make your game too easy. In this article, we’ll go over how to make temporary power-up effects.
2D Space Shooter Example

In my 2D Space Shooter project, I have a powerup called Triple Shot that allows a player to shoot 3 lasers instead of the normal 1 laser. To enable Triple Shot, the player must collect the Triple Shot power-up. When it’s collected, the powerup tells the player that the Triple Shot ability should be enabled.
Our goal is to find a way to turn off the Triple Shot ability during the game. Since the Triple Shot ability is controlled by a boolean value called _canTripleShot, we want to create a way to schedule _canTripleShot to become false. Since we are trying to schedule an action, we’ll use a coroutine to solve our problem.

We create an IEnumerator called DecayTripleShotTime that allows us to define a timeframe for how long we want the Triple Shot ability to be on. We’ll create a private instance variable for the timeframe and call it _tripleShotDuration.
Now, when we turn on the Triple Shot ability, we also call our coroutine. Once the allotted time passes, we reset our boolean _canTripleShot to be false and wait for the next time a player collects the Triple Shot powerup to turn on the ability. Pretty cool!