Chapter 16: Creating Modular Powerup Systems
2 min readOct 31, 2023
Steps to create the Triple Shot power up in Unity:
- Drag the laser from the prefab folder into the Hierarchy so it appears the scene.
- Duplicate it twice.
- Position the new lasers on the wings.
- Make sure the Y values are the same (-0.3) and the X values are opposites (-0.79 and 0.79) so they instantiate at the same position for each wing.
- Create an empty GameObject and call it Triple_Shot.
- Drag three lasers into the Triple_Shot object.
- Drag the Triple_Shot object into my Prefab folder.
- Delete the Laser in the Hierarchy to remove from scene.
A Modular Powerup System is more efficient way to add more power-ups to a game than creating a brand-new script for each power-up. Creating a modular system requires a switch statement. Switch statements can be cleaner and more readable than using a long series of if-else statements.
- Find the Triple Shot and Speed Boost sprite in the Project folder and move it up into the Hierarchy and name them Triple_Shot_Powerup and Speed_Powerup.
- Move the power-ups to the foreground in the sprite renderer and add all the need components. (Rigidbody 2D, Circle Collider 2D, or Box Collider 2D)
- Move them into position at the top of the screen and resize to 0.5.
- Making sure IsTrigger is checked in their Box Colliders and the rigidbody’s gravity set to 0.
- Create a Power-up Prefab folder and drag the two powerups into the folder in the Project window.
Now that the power-ups are created in unity write the modular systems for the powerups using a switch statements. Then code using a coroutine in the player script.