Added Simple Combat AI to Enemies

PieceofPhie
4 min readApr 12, 2021

Today I worked on adding simple firing functionality for the enemy.

I begun by duplicating my existing laser prefab and creating a special laser for the enemy. I name it ELaser (short for enemy laser ) then change up its color and appearance to be distinguishable from the player.

I decided to design it to be simple since they’ll be more enemies and their projects than anything that belongs to the player. I want to be mindful to how the game’s space is populated and not over crowding my game’s screen.

In addition, the player’s laser color is changed to the same color. Yellow seemed much more appealing than the illuminated candy red.

Enemy Script

I hop into my enemy script and establish the following:

I establish a variable that’ll handle the rate of fire, I declare a game object to be instantiated, create a method that’ll instantiate our object, and a method that’ll damage the enemy.

Two variables were declare to satisfy our need to handle fire rate and a object to instantiate. EnemyFireRate(if may have guess) handles the rate of the lasers coming out of our enemy. Elaser is a game object variable that we will assign via inspector and will be used to instantiate in our fire method.

Our new EnemyFire() method will check if the enemy is dead and is greater that 0 in the Y direction.

The enemy will only fire if its both alive and above 0 in the Y coordinate.

Next it’ll check if the elasped time is greater than our fire rate variable. Our EnemyFirerate is set to 1 by default and it takes about less than one second to begin the game. So the statement will return true until its fully executed.

Once executed, we will instantiate our ELaser gameobject and give it a rotation of 0. Then we assign EnemyFireRate the amount of time elasped plus a random number between 0.3 to 2.

For this demonstration, I assign it a random number so add of bit of randomness to when the enemy fires. I have a lot of enemies spawning so having them all shoot at once may seem too much.

EDamage

This block of code was previously called whenever the enemy made contact with a game object tagged laser. Not separating this code is what caused our enemies to be suicidal. I removed the block of code from our on trigger event so it may be called elsewhere.

The fire is then called in update.

The enemies shoot as they’re supposed to but theres additonal steps to get this working using the same laser script. The laser script was previously used only by the player. So lasers are only coded to do damage to the enemy.

I make sure I create new tags for my Elaser and Enemy. I do this so I can distinguish them when calling them during an on trigger event.

LaserBehavior script

I removed my collision check in my enemy script and consolidated it into the LaserBehavior script with the player collision check.

They’re now two collision checks that checks for the player and enemy. Once collided, it ll communicate to the collided game objects script and call a damage function. Then destroy itself.

The damage function is passed a int that is used to adjust the amount of damage the laser will do to the game object.

The Lasers from both the player and enemies are working as intended. It instantiates and destroys itself when necessary.

As you may have seen, theres code that allows me to assign the amount of health an enemy has aswell as the amount of damage it will do. Im currently working on a framework that’ll allow me to add different enemy types. More on that perhaps another day.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

PieceofPhie
PieceofPhie

Written by PieceofPhie

Unity Developer Based out of California

No responses yet

Write a response