New Enemy: Aggressive

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.




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

OCI | Database Cloud Service | Tutorial | Create PDB

Spotify Download Hoeveel Mb

Dual Booting Windows 10 with Ubuntu — Mission MSI

What is Kotlin? Why Kotlin? is Kotlin worth learning in 2020? Kotlin vs Java

Kotlin logo top of rock near river and trees

Building easy apps using MIT App Inventor and sensors on your phone

Screenshot of the list of sensors available on MIT App inventor

Philosophy of UNIX Development

5.2 Cards and Colors

How to import data from csv file and from APIs into MySQL database using PYTHON

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Kesler

Thomas Kesler

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

More from Medium

Unity For Beginners

I think you dropped something — Creating a simple loot drop system in Unity2D

Unity Development — The Resources Folder

Adding enemies to our greybox Prototype