A Nasty Mine Dropper Enemy Joins The Game

Andrea Zilio
3 min readJul 4, 2021

In the last post we made the game easier for our lazy players. Now let’s make it harder for those who like more challenges. We will add an enemy who drops a mine.

Creating the prefabs

To start we need to create the prefabs. To keep it simple we just reuse our existing enemy and modify him a bit, changing the color and size slightly, and turning him around. At a later stage there will be time to change the visuals for the whole game. We drag an existing enemy prefab to the hierarchy, then rename and modify it.

We now need a mine. There are free sprites on the web, but simple graphics like this can also be created in Photoshop. I created the image with two states (layers) and imported the Photoshop PSB using the Photoshop Importer (add it from the package manager) and from there made a prefab. The two images where then added in the animator to create a blinking mine.

Spawning the new enemy

There are many ways to do this. For now we are simply going to spawn the new enemy through the spam manager script. And then give it some behavior in the enemy script. As this enemy is going to move horizontally and just drops mines, we do not want more than one of these enemies at any time.

Ok so let’s write the code in the spawn manager first.

We create a reference to our new prefab, so we can assign it in the inspector into the Spawn Manager later. We will also need a boolean variable to check if an instance of this enemy exists.

Now it is time to instantiate our new enemy with a coroutine.

To do so we can adapt the code from our first enemy. We will create a balanced spawn manager later. There will always just be one of these new enemies at the same there. A boolean variable makes sure this is the case.

This new enemy simply moves horizontally to the right. The movement is implemented in the new enemy script.

Droping Mines

It is time to instantiate the mines. To keep it simple each enemy has a limited amount of mines. Thus there is a counter. These mines drop at different heights, using a Random.Range and remain there.

For now the audio and visual effects remain the same when the mines or the new enemy is killed. If our space ship touches the mines it takes damage. All of has been implemented for the first enemy already and we can simply reuse it.

As the goal is to add more enemies and features, it makes sense to spend some time on cleaning up the code and creating a reusable class “enemy”. Thanks to inheritance we can then reuse the code which applies to all the enemies rather than repeating it in each enemy script.

--

--

Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.