2D Galaxy Shooter P2: Enemies can Shoot Powerups!

Gabriel Perez
May 14, 2021

--

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

Heyyyy! Why did you do that for? Let me at you!

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

--

--

Gabriel Perez
Gabriel Perez

Written by Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

No responses yet