Implementing a Negative Powerup
Objective: Learn how to implement a powerup that negatively impacts the player.
So now we want to make our players be careful with what they chose to get as a powerup. We are going to add one that has a negative effect to our player.

So our negative powerup will slow our player movement speed, and prevent him from using his Thruster System. While affected by the slow, picking the Speed powerup also will not have any effect, so the player will be left moving as slow as it can be.
So we’ll start with our Player this time. Since this means the player will be negatively impacted, instead of changing the value of all numbers, such as speed multiplier, player speed, etc, what we are going to do is simply create a new variable that will let us know if the player is currently slowed.
private bool _isPlayerSlowed = false;
With our variable ready for us to use it, lets check how this is going to work.
private IEnumerator EnableSlowRoutine()
{
_isPlayerSlowed = true;
if(_powerUpSpeedMultiplier > 1f)
_thruster.SetActive(false);
yield return new WaitForSeconds(5f);
if (_powerUpSpeedMultiplier > 1f)
_thruster.SetActive(true);
_isPlayerSlowed = false;
}
A very simple Coroutine, that will change our variable to true, and after a few seconds, change it back to false. The if statements just deactivate our thruster visual, and reapplies if the player still had the speed powerup active.
public void ActivateSlowPowerUp()
{
StartCoroutine(EnableSlowRoutine());
}
Our public method, to be accessed by our Powerup script when its collected.
Now, how do we make sure that our player cannot use the Thruster System when he is slowed ?
if (Input.GetKey(KeyCode.LeftShift) && _isPlayerSlowed = false && _powerUpSpeedMultiplier == 1f && UIManager.Instance.CanUseThrusterBoost())
{
_isThrusterActive = true;
_thruster.SetActive(true);
StartCoroutine(UIManager.Instance.ThrusterBoostSliderDownRoutine());
}
else
{
_isThrusterActive = false;
if (_powerUpSpeedMultiplier == 1f)
_thruster.SetActive(false);
StartCoroutine(UIManager.Instance.ThrusterBoostSliderUpRoutine());
}
By adding our variable inside our if statement, we make sure that if the variable is true, we never go inside the statement.
private void MovePlayer(Vector3 direction)
{
RestrictYPosition();
TeleportOnXAxis();
if (_isPlayerSlowed)
{
transform.Translate(_playerSpeed / 2f * Time.deltaTime * direction);
return;
}
if(_isThrusterActive && _powerUpSpeedMultiplier == 1f)
transform.Translate(_playerSpeed * _thrusterSpeedMultipler * Time.deltaTime * direction);
else
transform.Translate(_playerSpeed * _powerUpSpeedMultiplier * Time.deltaTime * direction);
}
Now inside our MovePlayer method, before we reach our Translate, we check if our player is slowed. In case that is true, we apply a translation, with the player speed halved, and them simply return, preventing from continuing the method.
We have to make sure our SpawnManager is properly updated with the new prefab.

Now we need to make the adjustments inside our Powerup script.
private enum PowerUpType
{
TripleShot,
Speed,
Shield,
Ammo,
Life,
Special,
Slow
}
private void ActivatePowerUp(Player player)
{
AudioSource.PlayClipAtPoint(_powerUpClip, transform.position);
switch (_type)
{
case PowerUpType.TripleShot:
player.ActivateTripleShotPowerUp(_duration);
break;
case PowerUpType.Speed:
player.ActivateSpeedPowerUp(_duration);
break;
case PowerUpType.Shield:
player.ActivateShieldPowerUp();
break;
case PowerUpType.Ammo:
player.GrantBonusAmmo();
break;
case PowerUpType.Life:
player.GrantBonusLife();
break;
case PowerUpType.Special:
player.ActivateSpecialPowerUp();
break;
case PowerUpType.Slow:
player.ActivateSlowPowerUp(_duration);
break;
default:
break;
}
}
We have to make sure our enum contains the new powerup, and inside the ActivatePowerUp we simply call the activation of it, by telling our player script.

As soon as the powerup is collected, my thruster immediately went off, and I become incredibly slowed, making it very hard to avoid the incoming enemy. If it was later in the game, in the later waves, it might cost my run.
And there we have it, a powerup that cripples the player movement, making it harder to evade enemies is now in place!