Unity Dev: Determining How Long Power-up Effects Should Last
If it’s more than 4 hours please debug your code…
Okay, so we have three power-ups in the game so far.
- Triple shot (which you have seen) — triple lasers for a short time
- Shield — creates an impenetrable shield around the player for a short time
- Speed — increases movement speed dramatically for a short time
This is an interesting discussion in my opinion. If the power-ups spawn too frequently and the effect last too long, the game might be too easy. If they spawn to infrequently or the effects only last a few seconds, the player might not think the power-ups are worth going after.
In this game I plan to have the power-ups be rare treats that make a big difference in the game play. That way once the player know how important they can be, they will take risks to collect any power-ups that spawn.
For example; let’s image that the triple-shot only appears once during the span of one player life. If the player misses it they do not get another chance until their next life. If they manage to snag the triple-shot, it lasts until they take damage and lose a life.
Of course the options are limitless. We could create a difficulty selector on the main menu that changes the spawn rate and cool down times for the power-ups.
Reading back over this short article, I realize that I did not really give a solid answer on how long a power-up should last. I think it depends on what type of game, the type of power-up, etc. The key is that, as the game developer, we can make those decisions and possibly set our game apart from the rest.
Here is how I decided to implement this in my version of the Space Shooter 2D. I wanted each to be different and modifiable in the inspector for testing so I added a private float variable called lifeSpan to the Power-up script. This can be set in the inspector for each type of power-up.
Next, I changed the call from the power-up to pass the lifeSpan as a parameter to the function in the player script for which ever power-up was collected.
In the player script, the functions for the power-ups get the parameter and can then use it to set the cool down timer by passing that same value to the Coroutine’s waitForSeconds.
Quick note here about variables and SCOPE. The _lifeSpan variable in the first section of code (blue/white/green) is declared in the top of the power-up script and is available anywhere within that script. The lifeSpan variable in the second section of code above (orange/green/and ugly yellow) is declared as needed and only when the function is called. It saves the _lifeSpan value into the new lifeSpan variable and then passes that one to the IEnumerator Coroutine which creates a new AND DIFFERENT variable called lifeSpan to use as the cool down time. These are all real variables but they are not the same variable. Just like my name is Mike and you know a different someone named Mike (maybe). We are both Mikes but different Mikes. 😁 Cool, huh?
Tomorrow we will discuss making the power-up system more expandable.
See you there!