Creating a Modular Powerup System in Unity
We're going to be going over creating a modular power up system for our space shooter. a modular power up system allows us to add new powerups and implement their functionality more easily, let's get started.
Power Up System:
To get started creating the system were first going to create a power-up script to handle exactly what happens when the player collides with the power-up object. Let’s get started.
First, we Create a new C# script and name it PowerUp.
Now were going to need two add two variables a float for the speed of the power-up object and an int for the power-up ID.
- The ID will be used to determine what type of power up the object is so, 0 equals the Triple Shot, 1 equals the Speed Boost and 2 equals the Shield.
Inside the update were going to simply move the player using the Translate method multiplied by our speed. we are also going to check if the power up goes off screen at the bottom so we can delete it.
For the power-up check, we are going to use the OnTriggerEnter2D method and use a switch statement to determine what happens depending on the object’s ID.
Inside the player script, we need to create each public method for each power-up object. Let’s get started.
First, we need to create a few variables for our power-ups inside the player script.
Next let’s create the public methods that are called from the power-up script. We also create two Coroutines for the TripleShot and SpeedBoost power-up time, to determine how long they should last.
Next inside our spawn manager, we need to add some new variables for our power ups.
Now we are going to use a coroutine to spawn the power-ups. we are using Random.Range to generate a random time for the power-ups to spawn. we are using the same method to pick a random power-up from the array each time as well.
Thats it for the power up system.
Thank you for your time and attention.