Power Up Implication

Now that we have a power up spawning that the player is able to collect. Why don’t we look at how we set them up so that extra collects add to usage time instead of overriding it or meaning nothing at all.

When the player collects a power up that they are already using, they very likely expect or hope for one of two outcome, to extend the timer on the power up or to improve the power up further. For now, we will look at extending the time they can use the power ups.

One of the problems with Coroutines is that you can have multiple copies of the same routine overlapping itself. This can cause problems if you are using the coroutine as timer that flips a bool. So what do we do about it? We setup a timer value outside of the routine that we can change without entering the coroutine itself.

Let’s look at the Triple Shot Power up as our example.

Float used as a Timer Value
Triple Shot Power Up Coroutine

Here we have the Timer Value (set at the top of the class) and TripleShotRoutine() which controls whether the power up is active or not. The first thing that happens when we start the coroutine is that we set the power up to active, even before we start any loop because we don’t need to keep turn in it one every loop, just when it starts. Then while the power up is active we stay in this while Loop and since we don’t need to do anything else right away we will use WaitForEndOfFrame() before continuing. For this routine we aren’t using WaitForSeconds() because we will changing the Timer value potential between seconds and the FrameRate gives a finer increment to work with.

At the end of the frame, we subtract the Time.deltaTime from our Timer Value. For those that don’t know delta Time is the amount of change in the Time held by the game between the end of one frame and the end of the next frame. And since that processing can take longer on some frames than longer, this gives a way to pace things and add a sense of ‘Real Time’ to frame by frame actions. So while after one frame we may subtract only 0.01111f from our Timer, after 90 or so frames that will add up to a full second (presuming that the game’s frame rate is 90 FPS, the amount will adjust automatically for slower, faster or variable frame rates so that after 1 second the proper amount is removed.)

And that is all this coroutine does until the Timer value has dropped to or below 0. At that point, we hard set the Timer to 0 so that we don’t have any overflow hanging around when we restart the power up again. When We give the player a 5 second power up, they should get all 5 seconds of it.

Then we set the Active bool to false so that next time the coroutine tries the While loop, it fails and ends the coroutine.

Now let’s take a look back at our Switch statement case that starts the Routine. First, we add 5 to the Timer. We could set it to 5, but that would only reset the timer if the player collects multiples in a row.

Then we check to the Coroutine is running by checking the Active Bool that is only flipped on or off from within the routine itself. If it is False, then the last power up ran out and we need to restart the Coroutine. If it is True, then the power up is still running and adding to the Timer is all we needed to do to keep it running.

Next time, We will look at the Shield Power Up which increases ‘Hit points’ and is used up instead of running on a timer.



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
Thomas Kesler

Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.