Creating a Negative Power-Up!

Michel Besnard
5 min readApr 13, 2023

Objective: Create a powerup that negatively affects the player in a Unity 2D game project.

All is not well in the galactic battle space. The invading alien entity has unleashed a mysterious weapon, and intelligence analysis reports that colliding with it will negatively affect the ship’s life support system, its self-protection suite, and its weapon loadout. The only known defence is avoidance at all cost…

Let’s create this bad boy :)

Create an empty game object, then add a 2D Rigidbody and 2D Collider. Attach the PowerUps script (see the article on the modular power-up script used here) and set the Power Up ID to 7.

Add a child sprite image, duplicate it twice, and rename each as required.

Create and add the following script to the child game objects:

Adjust the color, sorting layer, and rotation speed for each of these children.

The image on the left is the final product, resulting from stacking the children (blackholeportal, blackholeportal2A, and blackholeportal2B) together.

Once you’re happy with your results, drag the game object into your prefabs. It’s now ready for use.

Let’s go back to the PowerUps script. Make life easy on ourselves by adding a comment to the _powerUpID private integer, stating that 7 equals the Negative PowerUp.

PowerUps script ~ ID descriptors in the comment section.

We then need to adjust the Switch statement to capture the possibility that the _powerUpID may equal 7 (case 7:), in which case we would execute the NegativePowerUpCollision() method inside the PlayerScript script.

PowerUps script ~ Adjusting the Case logic to call the NegativePowerUpCollision() method from the Player script when the Power Up ID equals 7.

Now that you’ve added the Negative Power-Up to your _powerUpID list, you need to go back to your SpawnManager, increase the size of the Health And Neg Power Ups array to 2 and add the NegativePowerUp prefab to the array.

SpawnManager ~ setting up the array to hold the Health and Negative power-ups.
SpawnManager ~ Inspector view.

In my version of the game, my Spawn Manager runs various coroutines to spawn various enemy ships and three categories of power-ups. I opted to combine the health and negative power-ups into a single array. Once called, a power-up is randomly picked and instantiated into the game.

SpawnManager script ~ health or negative power-up will be randomly selected from the array.

Once a collision is detected, the NegativePowerUpCollision() method is called in the Player script.

PowerUps script ~ switch statement for negative power-up.

The logic for the NegativePowerUpCollison() basically calls for a randomly selected system of the player’s ship to be affected. Areas to consider are ammo, missiles, energy core, or shields. Generate a random number between 0 and 4 (0, 1, 2, or 3), then run a Switch statement to execute the associated method.

Player script ~ NegativePowerUpCollision()

LoseAmmo() reduces the player’s ammo stores by a random quantity between 5 and 10. If this takes the total ammo count into negative territory, the _ammoCount is clamped at 0. The UI Manager then updates the data in the UI.

Player script ~ LoseAmmo()

LoseMissiles() runs a similar code, this time focused on the missile stores.

Player script ~ LoseMissile()

For more background information on the “core”, refer to the following article found here.

LoseCore() coroutine ~ One very unfortunate outcome of colliding with the negative power-up is to temporarily lose control of your ship’s core. This is the worst-case scenario since it leaves you floating in space unable to defend yourself for a period of time.

Player script ~ LoseCore() coroutine.

The coroutine basically turns off the thruster images and flips all the required bools for using thrusters and weapons to false. A few seconds later, everything comes back online, the ship rights itself up, and you’re back in business.

Note: Below is a variant if you want to have a less predictable “off” time:

Player script ~ LoseCore() ~ Right now it’s hard-coded for 5 seconds, but you could set up a randomly generated time span.
Player script ~ Coroutine that rotates the player ship back up when its “systems” come back online.

The final Case is for the player to lose its shields (if previously equipped/collected). The bool is set to false, an audio clip warns the player, and the shield game object is turned off. To view my shield-related article, follow the following link.

Player script
Player script ~ Damage()

I hope that you have found this article helpful and that it will assist you as you implement various features into your own Unity game project. Thanks for reading :)

--

--

Michel Besnard

Military member with 35+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.