New Enemy: Aggressive

Thomas Kesler
3 min readJun 11, 2021

It’s time to look at expending our selection of Enemies to deal with the Player.

For this enemy, we will be borrowing some ideas from the tracking Missile.

This enemy will start out as primarily a copy of our basic Enemy type. We will talk about Abstract Classes another time. For now, We will make a new script and copy the body of our Basic Enemy Class into the new script.

Let’s take a look at the changes we will need to make to get the behavior we are looking for.

First, by removing all but the highlighted lines of code, we can make it so the Aggressive Enemy will always target the player instead of just sometimes.

Then we add an new variable _shipSpeed and make it serializable. This will hold the default speed of the Enemy. In OnEnable(), we set (or reset) the _speed variable to _shipSpeed. This is because the Enemy can be destroyed when they are stopped.

In Movement(), we add two lines from our Missile class to give the Enemy the ability to always face the Player. transform.LookAt gives the Enemy a ‘perfect’ tracking ability and snappy response time to the Player’s movements. This doesn’t mean they will always hit the Player though.

To achieve the pause in pursuit by the Enemy, we will use the Coroutine above. This currently have the Enemy chasing for 3 Seconds and still for 5 Seconds to fire from a stationary position. The hard coded values on lines 5 and 8 can be replaced with serialized variables (_chaseTimer & _pauseTimer) to allow the designers more control over the attack pattern.

We return to OnEnable() to start the Coroutine of AttackPattern() so that it starts up as soon as the instance of the Enemy is spawned in and activated.

After this, we just need to subscribe the Classes that need to know about the Enemy’s death to the Event. When we go through later and refactor and optimize the game, we can combine the Enemies under an Enemy Abstract Class that will allow us to set a single EnemyDeath event instead of one for each Enemy type. For now, we work on making a working game.

--

--

Thomas Kesler

A Unity Developer with a fondness for Fantasy games and the challenge of pushing boundaries.