2D Galaxy Shooter P2: Enemies can Shoot Powerups!

For today’s challenge, I implemented the ability for enemies to shoot power-ups in front of them!

It was a simple implementation. All I needed to do in the PowerUp script was add a check for enemy projectiles in the OnEnterTrigger2D() function.

if (other.gameObject.CompareTag(“Enemy Projectile”))
{
Projectile[] projectiles = other.GetComponents<Projectile>();
for (int i = 0; i < projectiles.Length; i++)
{
if (projectiles[i].GetIsEnemyLaser() == true)
{
Destroy(projectiles[i].gameObject);
Instantiate(_explosionPrefab, transform.position, Quaternion.identity);
_audioManager.PlayExplosionSound();
Destroy(gameObject, 0.2f);
}
}
}

I also had to make sure the enemy projectile prefabs had the tag “Enemy Projectile” selected.

That was all! Every time an enemy is in front of a power-up and shoots, it will get destroyed. Easy peasy!

That is all for today! Thank you for your time!

Gabriel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store