Negative Powerups: Hazards (Game Dev Day 41)
Objective: Add “Hazards” to the powerup system that inflict negative statuses to the player.
NOTE: I shrank down the sizes on many of the Game Objects so the playing field would be less cluttered. If you have been following along, the changes in size are substantial, but intentional,
As of now, all the possible spawns from the Powerup Spawner are positive, and the player will want to collect them all. To deter mindless collection of the powerups, I added a negative powerup that temporarily freezes the player in place by causing an electrical shortage on their ship. This hazard also drops the player’s shield if they have one, leaving them open to attack.
First, I created a new GameObject from a sprite.
I added a rigidbody 2d (turn off gravity), a circle collider 2d, and the Powerup Script. Instead of using the powerup sound, I added a different audio clip to this one that sounds like an electrical fault. I also assigned the Powerup ID variable as 6 (as this is the 6th “powerup”, even though this one is negative).
I turned this object into a Prefab so it can be instantiated by the spawn manager in game.
Next, I set up the logic in the Player.cs script by creating a new bool (_isFreezeActive). I default this bool to false, so the player is not debuffed by default.
Next, I create a function that turns on the negative effect(FreezeActive).
This function will shut down any shields the player has and cancel any speed boosts, then activate the _isFreezeActive bool. After 3 seconds, it will deactivate the bool, which will restore the baseline speed values to their original values..
Now in Void Update, I add the “freeze” mechanic by turning the player’s speed to 0 if the _isFreezeActive variable is true.
You can test if the debuff is working by turning on the _isFreezeActive box in the Inspector on the player.
Now we need to get this effect attached to the actual Powerup Object, and make that object spawn from the Spawn Manager.
In the Powerup.cs script, I add another case to my array
Finally, I need to assign this powerup in the Spawn_Manager.
Then, in the Spawn_Manager.cs I increased the range of the powerup spawn integer to encompass the new powerup.
Let’s see it in game!