Geek Culture
Published in

Geek Culture

How to Incorporate Different Enemy Types into One Class

In my last two articles, I added a new enemy jellyfish to my game. The jellyfish was the second enemy added, and there are more to come. My objective for this article is to prepare my Enemy Class to take a variety of enemy types, rather than making multiple scripts for a variety of enemies.

In the Enemy Class, I make a new variable to store a whole number (integer value), and name the variable enemyID. Every enemy type will now have a corresponding number.

I currently only have two enemy prefabs (jellyfish and piranha), but it’s not too early to setup my enemy script to easily take more enemies in the future.

The image on the left showcases the enemy script being shown in the inspector on the piranha. The main difference to note between the script on the right for the jellyfish, is the Enemy ID. The piranha will now have an Enemy ID of 0, while the jellyfish holds an Enemy ID of 1.

Piranha enemy on the left and jellyfish on the right

In void start, I use an if else argument to differentiate between my enemies by referencing their enemy ID’s. This could of course be changed to a switch statement later on should this game become bombarded by a horde of enemies and a cleaner looking script it desired…sure. For now with only two enemies, the necessary calls are made in the first argument, only if the Enemy ID is zero (piranha). Then the else if argument handles all that is needed for the Enemy ID being one (jellyfish).

In void Update, I call a single Enemy Movement method. I then use another if else argument (see a pattern yet?), to check for the enemyID variable. In this case, the piranha uses it’s movement and it’s visual boundaries on the screen, in update. The jellyfish only uses the boundaries, being it’s movement is based on physics using a coroutine (check my last article).

The enemies share an on death behavior method, which once again breaks commands apart based on the enemyID variable.

The other class that will be very interested in my enemy ID transition will be the Spawn Manager. I previously had a GameObject variable to store the piranha, which I have now deleted. Instead, I introduce a GameObject Array[], which can store an infinite amount of enemies. I use the [Serialize Field] command, to show this private array in the Unity inspector, so I can drag and drop the enemy prefabs into their respective array positions (piranha = 0, jellyfish = 1). If the variable was public, there would be no need to serialize the field. I do not want my enemies spawning at the same position, so I introduce three Vector3 location variables; one for the piranha, one for the jellyfish and one to be reassigned between the two.

Here we go, in the coroutine where I spawn enemies for as long as the player is alive, I need to address the new enemy array. I assign spawn positions to the variables holding the piranha and the jellyfish. The local variable randomEnemy is introduced to cache an integer variable between 0 and 1 for the two enemies, with a 50/50 dice roll between the two. The enemies.Length command checks the enemies array and returns the maximum amount of enemies in the array. This way as I add more enemies, I don’t need to come back here to increment the hard-coded data for every new enemy. In the final paragraph, the randomEnemy variable is inserted into the enemies[] array. This will dictate which enemy in instantiated (created). The enemySpawnPosition variable now handles whichever position is returned from the enemies array, and the enemyRotation returns one of two rotation assignments. Finally, the yield statement waits for the allotted spawn rate time before starting again and instantiating a new enemy.

Now that I have a few enemies, I can start giving them some additional abilities to make the game even more challenging for the player. I hope you join me on further endeavors in future articles. Thanks for reading!

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Intro — About Me, Max Colbert

The minimum to know to work in an Agile team

INX: AMA with Shrimp_chat

CS50 — tutorial 9 flask, ajax

TA505 adds GoLang crypter for delivering miners and ServHelper

date command in Linux

Getting Started with WSL

Setting up Azure Active Directory for web app

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
Jared Amlin

Jared Amlin

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.

More from Medium

Daily Progression: Player Setup!

Adding a burst laser powerup

2D Mobile: Loot System

Journal 143 — Unity Project Development, 2.5D Shooter