Creating Modular Powerup Systems in Unity3D
Objective: We will create a Speed Boost power-up that will increase the movement speed of the Player GameObject. We will also reuse the TripleShotPowerUp script since the movement behavior will be the same, but we will need to rename the script to PowerUp as well as the Class within the script. Lastly, we will create a modular system to activate the power-ups.
Step 1: Create the Speed Power-up GameObject
Using the Speed power-up Sprite provided by GameDevHQ, drag one of the animation frames from the Project window and drop it in the Hierarchy window. Then rename the GameObject to Speed_Power_Up and in the Inspector window, reduce the Scale to X = 0.4, Y = 0.4, Z = 0.4.
Drag the Speed_Power_Up GameObject from the Hierarch window and drop it in the Prefabs folder in the Project window. Then delete the Speed_Power_Up GameObject in the Hierarchy window.
Select the Speed_Power_Up GameObject in the Project window. In the Inspector window, add a Box Collider 2D component, checking the Is Trigger box, and add a Rigidbody 2D component, setting the Gravity Scale to 0.
Step 2: Change the Name of the Power Up Script
In the TripleShotPowerUp script, rename the class to PowerUp and save the script.
In Unity, rename the TripleShotPowerUp script in the Project window to PowerUp, so the class and script name match, and drag it onto the Speed_Power_Up GameObject in the Hierarchy window.
Step 3: Create IDs for the Power-Ups
In the PowerUp script, create a private int variable named _powerUpID, utilizing [SerializeField]. Then save the script.
Since this script is attached to both the Triple_Shot_Power_Up and the Speed_Power_Up GameObjects, we can assign a number value to each GameObject that we can reference in the PowerUp script.
In Unity, select the Triple_Shot_Power_Up GameObject. In the Inspector window, verify that the Power Up ID is set to 0 within the Power Up (Script) component.
Next, select the Speed_Power_Up GameObject and change the Power Up ID to 1.
Step 4: Create the Speed Power Up Behavior
In the Player script, create a new private float variable named _speedBoostSpeed and have it equal to 7.0f.
Then create a private bool variable called _is SpeedBoostActive, which will be set to false by default.
Create a new public method called SpeedBoostActive(), which will set the _isSpeedBoostActive variable to true and start a new coroutine called SpeedBoostPowerDownRoutine(), which will yield for 5-seconds, then set the _isSpeedBoostActive variable back to false.
Inside the CalculateMovement() method, create an If statement stating, if the _isSpeedBoostActive variable is true, use the _speedBoostSpeed variable within the transform.Translate statement. But if the _isSpeedBoostActive variable is false, use the original _speed variable.
Step 5: Create a Modular Power Up System
In the PowerUp script’s OnTriggerEnter2D() method, within the null check if statement, use a switch statement to call each power-up method from the Player script, using the power ups’ Power Up ID as each case assignment. Then set the default to post an error message saying, “Default Value”.
Save the scripts, go back to Unity, drag a Speed_Power_Up Prefab into the Hierarchy, and set its position to the top of the screen. Play the scene, and when the Player GameObject collides with the Speed_Power_Up Prefab, the Player’s speed will increase for 5-seconds. Also, when the Player GameObject collides with the Triple_Shot_Power_Up Prefab, the triple shot will be active for 5-seconds. This verifies that the modular power-up system is working correctly.