My 120-day journey to being a better Unity Developer — Day 18
Creating Modular Powerup Systems
Objective: Creating a modular powerup system that provides the ability to add more powerups with ease
PowerUps
An enum is used that associates the name of each powerup TripleShot, Speed, or Shield to an ID 0, 1, and 2. This way we can use the name, yet the code in the background will use the IDs. This is the first place where a new powerup should be added to get a reference ID.
We then add this new field powerUp to the Powerup class.
The Power Up script in the Inspector provides the ability to pick the Powerup Type.
Spawn Manager
The next change made was to the Spawn Manager were the single powerup variable was changed to a collection of powerups (array).
A SpawnPowerUpRoutine coroutine method was created to randomly select a powerup and instantiate it.
Powerup Script
Now when it comes to the OnTriggerEnter2D method, it checks which powerup touched the player by referencing the powerup ID. It then passes this ID and matches it to the section of code in the Switch statement.
Now we can add as many powerups we need and their behaviours.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.