If one power-up is good, then it stands to reason that more power-ups are even better. In my previous article, I described a way to keep your power-ups from lasting forever. This time around, I would like to dive into creating different kinds of power-ups modularly.
I have a power-up called Triple Shot. It allows the player to shoot three bullets at once. Now, I want to give my player the ability to collect a Speed Boost.
I could create a whole new Script for this new power-up, or I can devise a method from making my current power-up act in different ways depending on certain conditions.
Let’s work on the latter.
Convert my current power-up script into a modular system for implementing different power-up behaviors.
Currently, my power-up Script calls a method in the Player Script to activate the Triple Shot behavior. What this power-up does, is controlled by the Player Script. The Speed Boost will be no different.
First, I will define what the Speed Boost power-up will do in my Player Script.
The basic concept for this power-up is the following:
The public method named ActivateSpeedBoost will get called by the power-up. It will then start a Coroutine called SpeedBoostRoutine. Inside of this Coroutine, I generate a cached variable of the current Speed value named baseSpeed. I then set the value of the Speed variable equal to SpeedMult multiplied by Speed. The following line suspends the execution of this Coroutine for a specific number of seconds. Finally, when the Coroutine resumes, I reset Speed to the original cached value.
My player will now be able to move faster if they pick up a Speed Boost power-up. The effect will last for five seconds and then return to its base speed. I can control the length of the power-up’s duration with the SBDelay variable.
Back to the Power-Up Script. When the power-up GameObject detects a Collision with the player, it currently calls the Triple Shot method. We can change this by making a few changes to the Script.
First, we will define a way to distinguish one power-up from another. I can do this by creating an integer variable called Ptype.
I can use this integer to decide what effect my power-up has.
I can then alter the
OnTriggerEnter2D method to also check for this integer.
After making sure that the Player Component is not null, I use an If-else statement to decide which method to call. The PType variable can be set in the inspector to create a new version of my Power-Up.
After creating a duplicate of my power-up and assigning the new PUType, I also changed the visual aspect of the GameObject. I replace the sprite with something more fitting.
And lastly, here they are during gameplay.
Creating different power-ups is limited by the scope of the game project and our imagination. Creating a modular system for implementing game mechanics will help us expand our game at any point in time. This kind of flexibility is something we should always try to implement.
Did I mention that ‘modular’ is one of my favorite words?