Phase 2: Negative Pickup
Another day and another challenge, but this one will affect the player in a negative way. Today’s challenge is to create a negative powerup.
Create a powerup that negatively affects the player.
This is another one of those really easy to implement features since we have a modular powerup system and it’s very easy to add more powerups on an ever-growing system.
Before we dive into this article I need to warn you I’ve replaced all of the image assets with something I found online that looked a lot better for me and that had quite a variety of enemies and more.
Creating The Powerup Logic
I’ve decided that the negative powerup will reduce the player’s speed quite a lot that it’s barely able to move around, but still being able to use the shift key for a short thruster boost. Since the actual powerups are on the player I decided to start there and work my way to the actual powerup script. I’ve been using a switch case for the powerups so it’s important to keep track of the case ID’s and make sure that they’re the same on both scripts for easy referencing.
I’ve created a new float variable called “_negativeSpeed” and set the value to 4, I then created another float variable called “_negativSpeedTimer”, and a bool variable called “_negativeSpeedActive”. The base player speed is 5, and when the player picks up the negative powerup (debuff) it will minus the player _speed (5) with the _negativeSpeed (4) resulting in a new speed of 1. When the debuff is activated it will start a cooldown Coroutine that will simply plus the player _speed (1) with the _negativeSpeed (4) to return the player to its original base speed of 5 after the cooldown is finished.
Now it’s time to create another switch case but this time in the Powerups script. On our Player script, the speed debuff case has an ID of 6 and I’ve made sure that the case in the Powerups script also has an ID of 6. All we need to do now is active the powerup on the player.
Creating the Actual Powerup
Now that we have the logic in place it’s time to create the actual powerup. What I’ve been doing every time I need to create a new powerup is select an existing powerup and unpack the prefab completely. This allows us to make changes that won't overwrite settings on the existing powerup. This can be done by dragging a powerup into the Hierarchy. I then right-clicked the prefab and clicked on “Unpack Completely”. I then changed the name of the powerup to “NegativeSpeedPowerup” and set the “PowerupID” to 6. This ID corresponds with the powerups in the powerup script, Player script and the actual powerup. This is important in case we need to debug something later on. We now need to replace the sprite image with something else that would give the impression that the powerup is not a good one, but you might want to mask it that would trick the player into collecting it. When everything is set up we can then drag the powerup to our prefabs folder to create a new prefab that we can assign to our SpawnManager.
Once everything is done we can now test our new debuff powerup.
Again we can see how beneficial it is to create modular systems that we can add to without a lot of effort. Every time we’re just revisiting parts of our scripts to build on our existing functions without having to create an individual script for each powerup we create.