Road to Game Dev: Dangerous Enemies

Rowan Mcmanus
3 min readJul 25, 2021

Yes, they fight back now! Let’s take a look at how we want to make these enemies even more dangerous, instead of only being able to mindlessly ram into you. We’ll be revisiting this topic multiple times as we make enemies more and more intelligent, but for now, let’s focus on just making them shoot at you.

Creating the Enemy Fire Controller

We’re going to go back to one of the very first things we explored on this journey and take a fresh look at our old fire controller. This time, however, instead of waiting for player input, we’re going to have the enemies fire on a timer, with a little bit of randomization:

Bonus points: Rather than creating a brand new laser script, we can just adapt our existing laser and use that. However, this causes a few problems if we just directly use those lasers. Here’s what happens:

Yep. They just self destruct as soon as they fire. Obviously, that’s not quite what we want to have happening. So, let’s look at what we need to do for our laser.

Creating Multi-Purpose Lasers

The first thing we need is a way to set whether or not this laser is one that we made, or one the enemy made. This isn’t too difficult; All we need is a “hostility” variable and then a set of public functions we can call to set it and retrieve it.

From here, it gets a little bit more difficult, but not too much so. We need to make sure the laser is heading in the intended direction, which we can just do in the laser itself fairly easily by just reversing the vertical direction if it’s hostile:

Now we need to actually make use of that hostility for damage. Both our Player and Enemies will check whenever they collide with a laser and take damage from it; now we need them to first check if the laser is hostile or not. Now it’s as simple as calling that allegiance tag:

And there we have it! We can use this same technique to start making a lot of things in the game smarter — including allowing enemies to destroy our powerups…

--

--