Adding Features: Nuke

Austin Hall
3 min readJan 31, 2023

--

For This feature I wanted a bomb that would destroy all of the enemies on the screen simultaneously.

I want the bomb to behave in the same way as the powerups. That is, I want it to spawn randomly during the game, and have the ability to be collected by the player.

First, I created the bomb, and attached the powerup script to the object. I gave it a capsule collider and a rigid body 2D and I assigned a powerup ID of six.

After I was satisfied with the bomb object, I dragged it to the prefabs folder to create the prefab object.

From the inspector, we can select the spawn manager and add the Nuke prefab to our powerup array.

We will be working with three scripts for this feature. The Enemy, the Player and the Powerup scripts.

ENEMY SCRIPT

Until now, we used the collision with the enemy to control the destroy function, animation and sound FX. For the Nuke, there is no collision with the enemy, so we need a way to control that Destroy function from another script.

To do that I created the Destroy method. When this method is called, the object will be destroyed, complete with animations and sound FX.

The Destroy method allows us to destroy the enemies properly using cross script communication. Prior to this, collision with the enemy was required.

PLAYER SCRIPT

We need the player to interact with the bomb in the same way that it interacts with the other powerups. Except, this time when we collect the bomb, enemies go boom.

I created the Nuke method.

We create an array called enemies. We find all of the objects in the scene with the tag “Enemy” and store them in our array.

Using a “for each” statement, we access all of the objects in our array.

For each object we get the Enemy component and assign it to the variable “target”.

This allows us to use cross script communication to access the Destroy method on our enemy script.

Each enemy in the array has the script assigned to them individually. when the destroy method is called in the “for each” statement, it will destroy each object in the array.

POWERUP SCRIPT

All that is left to do is add the case for the nuke into our switch statement.

we use case six because our powerup ID is six.

and BOOM!

--

--

No responses yet