When 1 enemy isn’t enough.

Objective: Adding in new enemy types.

Rose Owen
4 min readJun 25, 2021

All we have now is the 1 enemy type that we have been using all this time, we can add some diversity and challenge by adding in extra in different kinds of enemies.

Minelayer:

In this case, we can use the movement we used in the last article or make a new movement using the same method. Then just add a different laser, in this case, a mine, and maybe even add some homing functionality.

This one can reuse a lot of code but will need to add some code for the mines to detect the player. After making/finding a sprite to use, we can add the sprite, an animator that is using the enemy animation controller, a Rigid body2D, a Box Collider 2D, and the enemy script.

You might have noticed under the Enemy Laser, it says mine instead of laser, this is so the enemy drops the mines instead of shooting a laser. Apart from that, it looks a lot like the normal enemies we have already made. All that is left now is to make the mine.

This one was made like the homing missiles, having an empty that follows the player and contains the mine, which will be what does the damage. All that is needed on the empty is a Circle Collider 2D and the Homing detection script from the homing missile. Now for the actual mine, adding the sprite, circle collider 2D, rigid body 2D, and tag as an Enemy laser.

With that all set up, we can add in some lines of code for the homing mine on the homing detection script, this is because at the moment it is only set up for if it finds the player.

Here we can add in some extra variables, a bool for if it is a mine, another bool for if the player has been found, a transform for the player, and we can put a #if / #endif around the thrusters GameObject, this will stop a null reference exception since the mine doesn’t have a thruster.

The bool for mine is Serialized so we can set it in the inspector, Player Found is for testing purposes.

Next, we can put an if statement around when the enemy is found and set it to only get called when it’s not a mine.

Now to check if it is the mine and then to check if a player is found or not. If the player is found then we can start to follow the player and so the player isn’t trying to dodge the mines non-stop, add in a coroutine to explode the mine.

Let’s start on the coroutine first, all that is happening in here is waiting for 5 seconds then Instantiating an explosion and destroy the mine.

Now for the following the player when it’s spotted. The first thing to check is to check if the player is null if it is set player found to false. If the player isn’t null we can then calculate the direction to the player by using the player's position and taking the mine’s transform from it, next we can normalize it and start to move towards the player using the Translate method.

Last we need to be able to set the player’s transform. In the OnTriggerEnter we can check if what we went into is the player. If it was the player, we can set the transform of the player and set the player found to true.

And with that, it’s done. Another feature added, in this case, a new enemy type. As always testing things out to see it’s working as intended and see you in the next one!

--

--

Rose Owen
Rose Owen

Written by Rose Owen

Game Developer in the making~

No responses yet