2D Galaxy Shooter — Making the Enemies Shoot
Hello all!
Today, I will go over how to make the Enemy shoot at our player! It will only be shooting down when it starts, then every 3–7 seconds. This has already made the game a little harder, I can’t wait to add more challenging enemies!
Making the Enemy Laser
All I did was duplicate and manipulate the laser I had by scaling it on the x axis, renaming it Enemy_Laser, changing its tag to Enemy_Laser for the collision, and making it a prefab. Easy enough!
- If you don’t change the tag, its own laser will kill the enemy! Although hilarious, it wouldn’t make a good game.
Customize it the way you want, there are so many ways to do this!
Enemy_Laser Behavior
- Create a new C# script. Do not duplicate a script, it causes some weird error message on Unity that repeats.
- Name it EnemyLaser.
- Add it to the Enemy_Laser prefab.
- Open both Laser and Enemy_Laser.
- They will both be basically the same, just switching directions.
- Change the _speed to -8.0. This just switches the direction the laser goes.
- Change the if statement that destroys the laser after it does off the screen. Make it destroy itself after leaving the bottom of the screen.
- Remember to change the sign from Greater than to Lesser than.
How the Enemy Shoots the Laser
Now that the laser moves down and won’t kill the Enemy, let’s make it where it shoots down towards the Player using coroutines! They are used a lot in this game.
- Open the Enemy script.
- Just for future debugging, create a new method called CalculateMovement(), then move everything from Update() there. Let’s keep the movement code separate.
- Call CalculateMovement() in Update().
- Create variables:
private bool _isEnemyDead = false;
[SerializeField] private AudioClip _laserClip; - Assign your laser clip sound to the Enemy script.
- Create a new IEnumerator called FireEnemyLaserRoutine():
6. Remember to StartCoroutine in the Start() method.
Damaging Player with Enemy_Laser
The laser would be pointless if the Enemy can’t hurt the Player!
- Open the Player script.
- Add the method above!
This was how to make your enemies shoot their own lasers! Next, I will make it where the Player’s shield will be able to take 3 hits!
Thank you for your time!