Secondary Fire Powerup

Renato Figueiredo
3 min readDec 14, 2023

--

Objective: Create a new powerup that helps the player destroy his enemies.

We have a lot of powerups for our game, but how about we make a new one ? And how about we make this one Special ? Well, instead of adding a small feature to our player, this powerup will destroy all enemies in the screen.

We are going to start with the creation of our new powerup.

Creating our Special Powerup.

With our powerup created, now we need to change the code for our Powerup script.

private enum PowerUpType
{
TripleShot,
Speed,
Shield,
Ammo,
Life,
Special
}

With this now we can select the PowerUpType in our Inspector, and set it to Special.

Setting the Powerup Type in the Inspector.

But how do we go about destroying all enemies in the screen ?
Remember when we made sure that all enemies are instantiated with our Enemy_Container as their parent ? That means that our SpawnManager can get the information for all the children inside our container!

public EnemyBehaviour[] GetAllEnemyBehaviors()
{
EnemyBehaviour[] enemyBehaviours = GetComponentsInChildren<EnemyBehaviour>();

return enemyBehaviours;
}

This public method allow us to access our SpawnManager and get an array of EnemyBehaviour scripts, which later we can use to tell the enemies to be destroyed.

public void ActivateSpecialPowerUp()
{
var enemies = SpawnManager.Instance.GetAllEnemyBehaviors();
foreach(var enemy in enemies)
{
enemy.TakeDamage();
}
}

Inside our player, we create a method that simply get all EnemyBehaviour scripts that are currently inside our Enemy_Container, which should contain all enemies that are currently alive.
When we have that information stored in a variable, we just do a foreach loop, running through all items of our variables, and when inside each item, we just call our public method of TakeDamage from our enemy.

private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Player player = other.GetComponent<Player>();
if (player != null)
{
ActivatePowerUp(player);
}
Destroy(gameObject);
}
}
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;
default:
break;
}
}

We have to make sure to add our Special Powerup to our switch when colliding with our player.

Lastly, we have to adjust to make sure our powerup doesn’t come up as often as the others in our SpawnManager.

private IEnumerator SpawnPowerUpRoutine()
{
yield return _initialDelay;
while (!_isPlayerAlive)
{
float randomPositionX = Random.Range(_minXPosition, _maxXPosition);
_powerUpSpawnPosition.Set(randomPositionX, _spawnPositionY, 0f);
int randomPowerUP = Random.Range(0, _powerups.Length);
if(randomPowerUP == 3)
randomPowerUP = Random.Range(0, _powerups.Length);
Instantiate(_powerups[randomPowerUP], _powerUpSpawnPosition, Quaternion.identity, _powerUpContainer.transform);
yield return new WaitForSeconds(Random.Range(3, 8));
}
}

We reroll our Special powerup value once, requiring two consecutive rolls of 3 to obtain the Powerup, making it rarer than others.

Now when we get our Special Powerup, we tell the player to get an array of all enemies alive, and order them to destroy themselves!

Our Special Powerup in action!

Now our player can literally destroy all enemies with a single powerup!

--

--

No responses yet