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…
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.
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.
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.
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.
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.
Once a collision is detected, the NegativePowerUpCollision() method is called in the Player script.
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.
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.
LoseMissiles() runs a similar code, this time focused on the missile stores.
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.
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:
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.
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 :)