Day 18: Enemies Fire Back
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.
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.
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.
Why didnt I just create a new script?
I like to reuse things and dont like to execute more code than I need to. Plus creating a new script would mean another thing to keep track of. I like to keep everything in one spot as much as possible. Though, the code is getting a bit chunky and I may need to separate them in the future.
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.
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.
Till then, take care