Feature: Negative Power Up
This article will show how to make a Power-Up that will negatively affect the Player if it is collected. The negative effects will be the loss of one health and an increased fire delay for the Power-Up duration.
In the Player script, we need a few variables. The first is a float for the amount to increase the fire delay. The second is another float, so we know how long the negative effect should last, and the final variable is a bool, so we know when the negative effect is active.
In Start, we need to set the negative effect duration to the base Power Up duration.
In the FireLaser method, we need to implement the fire delay increase when the negative effect is active by adding it to _canFire.
The _canFire variable determines if we can fire, which is covered in the Cool-Down system article.
Next, we need a Coroutine to tick down the negative effect duration to zero, then reset it back to the base Power Up duration, and turn off the negative effect.
We need a public method that the Negative Power Up will call to activate the negative effect, immediately affect the fire delay, and start the Coroutine. But if the effect is already active, we will increase the duration.
In the PowerUp script, we need to add a new element to the Enum for the Negative Power Up.
And finally, add a case to the switch for the Negative element, where activate the negative effect and take one health from the Player.
In Unity, we need to create the Negative Power Up Prefab for the Player to collect.
In Play Mode, we can see the Negative Power Up effect working when the Player collects it.