Enemies Fire Back!
To make our game a little bit more challenging, we need our enemies to have a way to damage us, other than the player ramming into them. And that is where, naturally, more lasers come in!
First up, I have changed the color of the enemy laser Sprite to green using Photoshop. Next, I have created an empty object called ‘Enemy_Laser’ where I have put two laser prefabs, adjusted their transform, and saved it as a prefab.
We need to adjust the ‘Enemy’ script, to add the laser prefab reference and a similar system to the cooldown system on the player. Next, we call the ‘EnemyShoot()’ method whenever it is off cooldown. Notice that since we have two lasers being shot, I used a ‘foreach()’ method to call the ‘AssignEnemyLaser()’ method in the ‘Laser’ script, which we will be looking at next.
Now, let’s open up that ‘Laser’ script. Here we need to differentiate whether the instantiated laser is players or that of an enemy. We do this simply by adding a boolean variable and adding a method that will set this variable to true whenever the laser is instantiated in the ‘Enemy’ script.
Next, we have to do some slight tweaks so that the laser shoots down, not up… And now all that we need to do is check the collision with the player - and if the collision does happen, destroy the laser and call the ‘Damage()’ method on the player.
And voila! The player now has a new challenge - dodging all of those lasers that try to destroy us!
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!