Negative Powerups: Hazards (Game Dev Day 41)

Jack Leavey
3 min readSep 28, 2021

--

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.

This is the “Hazard” that the player collides with to produce the negative effect.

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).

Make sure to set the speed values back to their intended amount at the end of the coroutine.

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

Added Case 6

Finally, I need to assign this powerup in the Spawn_Manager.

Added Element 6.

Then, in the Spawn_Manager.cs I increased the range of the powerup spawn integer to encompass the new powerup.

Line 54, Changed the second integer from a 6 to a 7.

Let’s see it in game!

A sitting duck.

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet