Shooting some enemies
Now that I have the shooting down, it is time to add enemies to the mix!
I also decided to add some color to the game to differentiate between the player and the enemy at a glance.
Adding color to the cube was pretty easy. Create a material, select a color you like, and drag it onto the game object 😀
Having just finished the movement for the player and laser, getting the enemy movement correct was easy. I just set the enemy to travel downward, instead of upwards like the laser. Next, I make it re-appear at the top after having reached the bottom.
Detecting collision
To destroy an enemy it needs to be able to detect when it is hit. That’s what the collider component is for, and that was already attached when I created the cubes. To do that I need something called a Rigidbody.
Just by adding the Rigidbody component, the gameobject now has physics properties. By default, the gravity setting is on, which makes my enemy go wild. I’ll be disabling the gravity 😅
This makes the enemy crash into the player, all I need for it to do is detect the hit. There is a setting called “Is trigger” and, enabling that makes it behave more like a tripwire.
Using tags I can heck what has entered the “trigger zone”. If the tag matches “Laser” it will then destroy the laser, and itself.
The order is very important here. If the enemy gets destroyed before the laser, there’s nothing left to destroy the laser.