Core Challenge 8: Create an aggressive enemy that rams you!

Esteban Ibarra
3 min readMay 5, 2021

--

Meet Rammstein!

The next challenge was also a little easier since the idea is similar to the homing missiles. I took the opportunity to photobash another spaceship. If you like it, again feel free to use it in your games or projects. I’m putting it under Creative Commons.

Since we’re using switch/case for various enemies, we might as well recycle some of our current code, we’ll put a case 5 in our enemy codes Start() method.

We’ll be setting up our enemy to be a slower mover at 3–4 units per second. they can shoot and have a fire rate of 1 bullet every 5 seconds or so. OK! Before we get to the heart of the code, the ship will act in two phases. Phase 1, where it will behave like a normal enemy, and Phase 2 where if the player gets too close, the enemy ship will chase it down and try to ram into the player. So let’s create a variable for the phase.

a protected variable is one that can be passed down to children. I figure if there’s ever a case in the future where I may need this, I might as well make it available to child scripts that inherit from this one.

In Update, there’s a CheckMovementbyID() method. Our case will be #5, so we run a method called RammerMovement as well as do a check to see if we can fire lasers. If we’re in Phase 1, we can. Not so in phase 2.

So first we do a check for phase 1, if we’re in it, we’re going to rotate the sprite by -90. We need to do this in order for the homing function to work. Then we’ll move down by telling the function to move right. Remember we’re currently rotated so moving right will be essentially moving down.

We’ll check the bottom of the screen, if we reach it, we respawn at the top. This code’s been covered earlier so we won’t go over it.

If the player gets near the ship, we add one to the phase. We could set it equal to two, but since we’re in a constantly updated algorithm, there’s a chance phase could be set to 1 again and the ship won’t go after the player.

So once we reach the other phase, we tell the enemy not to shoot anymore and essentially head towards the players direction forever.

In short, we learn where the player is by subtracting our position from the players and that’s how we find out what direction to go.

Getting the Atan of a number is getting the angle from 0. So throwing the y and x directions into the atan function will give us the necessary angle for our enemy ship to face. However we are given the info in radians and we need degrees. Fortunately the Math function has such a tool built in.

After normalizing the direction and getting rid of the magnitude or length to go to reach the player, we put it into the rigidbodys movePosition function to physically move the ship toward the player.

Cool! Tomorrow we’ll do an enemy that can tell the player is behind them and shoot backwards!

--

--

Esteban Ibarra
Esteban Ibarra

Written by Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.

No responses yet