Day 21: Adding Extras — Enemy Fire
The goal of these articles is to make our game better. Without a challenge, video games are boring. Lets get our enemies to start firing at us!
I assume you already know how to create a prefab. Create an enemy laser prefab. Make sure to set their position properly!
In our Laser Script we need the following:
We also need a public function to set
isEnemyLaser to true.
Where are we going to Assign the enemy laser and set it to true? In the Enemy Script!
Here we are getting both the lasers inside the parent object (enemyLaser) with GetComponentsInChildren<>().
The for loop helps us easily loop through game data. So while
i is less than however many lasers we have, we are going to increment
i. Since I have 2 lasers in my Enemy Laser Prefab the for loop ends after incrementing twice.
As you can see here, the enemies are firing 2 lasers.
Last but not least, we need to check if the laser has collided with our player. I’m sure you know how to do this by now! If not, here is what I put in my Laser Script.
There we have it! Enemies are now able to fire lasers at us! Im sure you noticed the new enemy type in the GIF ;) We’ll get there! Until next time!