2D Certification: Smart Enemy

Christopher West
3 min readSep 6, 2022

--

In the last article, I implemented an aggressive enemy type. Today, I’m going to be continuing the second, and final, phase of the GameDevHQ 2D Game Development Certification track. In this article, I’ll be adding a smart enemy type! The requirement reads as follows:

Tasks:

  • Create an enemy type that knows when it’s behind the player, and fires a weapon backwards.

The first thing I knew I was going to have to do to complete this requirement was to move the Fire Laser behavior into another class so that I could make the firing behavior modular and easily added to any enemy class. To accomplish I move the FireLaser, FireLaserRoutine, and CalculateNextFireTime Methods and al the associated initialization code into a new class called EnemyFireLaser.

With the firing behavior extracted I could now create another class that provides different firing behavior for an enemy and choose which class an enemy type uses by simply adding the appropriate script. Our new smart enemy would need to use a new type of firing instead of the original version which only fires forward. To accomplish this I created another script called EnemyFireSmartLaser. This class has much of the same structure of my previous firing behavior, including FireLaser, FireLaserRoutine, CalculateNextFireTime, and initialization code. In this new version of the code I needed to know where the player is so that I can determine if I need my enemy to fire behind them. To do this, I used the FindObjectsOfType method to retrieve all of the player objects in the scene. I used the method that finds all players in the scene to accommodate multiple players if I decided to include that feature in the future. Once I had a list of players I looped over them and did some vector math between the players position and the position of the enemy that the script is attached to, to determine which player is closest to my enemy. Once I have my closest player I use their vertical position to determine if they are ahead or behind the player.

Once I know if the player is behind the enemy or not I change the lasers fire direction to accommodate firing ahead or behind the enemy.

Lastly I wanted both firing behaviors to provide a way for the enemy class to stop the firing. To this end I added a public method called DisableFiring that sets the boolean that controls if firing is active. I also extracted an IFireLaserBehavior Interface for both classes to implement.

Lastly I modified my original Enemy script to use the new DisableFiring method instead of tracking the enemy’s ability to fire within the class. I then attached my new scripts to all of my enemy prefabs, including the one I created for the new smart enemy type.

Next Time!

In this article, I implemented a smart enemy type. Next time, I'll be adding enemy pickups! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.

--

--

Christopher West
Christopher West

Written by Christopher West

Unity Game Developer, Software Engineer, Gamer, Musician, and Father. Christopher is a creative that enjoys a challenge and loves coding.

No responses yet