What would a Top-Down Shooter be without Power-Ups?! Giving the player high-risk, high-reward bonuses during gameplay can break up the monotony of simple point-and-shoot. How long should those power-ups last? The final decision is up to you, but I can show you how you can apply a time limit to them.
The purpose of a power-up is to alter an aspect of a game mechanic and improve on it. It can affect the strength value of the desired feature or change its behavior to give the player an advantage.
In my case, I have created a power-up that allows the player to shoot three bullets at once. This feature will give the player a wider area of attack ahead of it. However, I want to make sure that the advantage this power-up provides only lasts for a short time.
Currently, I can collect the power-up, and its effects will be permanent.
I certainly can’t allow my player to have such power for the entire game. They might lose interest or find it too easy. Honestly, I’m doing this for them.
The code for my power-up is the following:
Using a Trigger Collider, I check to see if my power-up has collided with the Player GameObject. When it does, the script gets a reference to the Player Component. Finally, using that reference, the power-up calls a public method in the Player Script.
On the Player script, I start by declaring a bool along with two separate GameObject variables. The bool will control the shooting behavior.
Update()method, I constantly check for user input for shooting.
If the user presses the spacebar, they will call the shooting method.
Further down, I have declared the method that controls shooting.
The shooting method checks to see if TripleShot is true. If that is the case, then it instantiates the Tripleshot GameObject. It then sets the new clone’s parent to be a Transform called BulletContainer. However, if the bool is false, the method will instantiate a regular Bullet.
Finally, I have defined the public method that controls the TripleShot bool.
We now need a way to allow this power-up to last for a set amount of time. With a few modifications, we can use a Coroutine to control it.
The code for the Coroutine is as follows:
I have defined a float variable to control the duration of the power-up more easily. Then, I created a Coroutine called TripleShotCooldown. Making use of a yield statement, the Coroutine will wait and reset the bool to false.
All that is left to do is to start this Coroutine when we first activate the power-up.
There are many different ways and conditions you could use to control how your power-ups behave. I have selected this simple method that defines the behavior for a length of time.
The result is just as expected. My player can collect the power-up, and enjoy being overpowered for a short while.