2D Galaxy Shooter — Making the Enemies Shoot

Marcus Cohee
2 min readSep 3, 2021

--

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

  1. Create a new C# script. Do not duplicate a script, it causes some weird error message on Unity that repeats.
  2. Name it EnemyLaser.
  3. Add it to the Enemy_Laser prefab.
  4. Open both Laser and Enemy_Laser.
  5. They will both be basically the same, just switching directions.
  6. Change the _speed to -8.0. This just switches the direction the laser goes.
  7. 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.

  1. Open the Enemy script.
  2. Just for future debugging, create a new method called CalculateMovement(), then move everything from Update() there. Let’s keep the movement code separate.
  3. Call CalculateMovement() in Update().
  4. Create variables:
    private bool _isEnemyDead = false;
    [SerializeField] private AudioClip _laserClip;
  5. Assign your laser clip sound to the Enemy script.
  6. 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!

  1. Open the Player script.
  2. 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!

--

--

Marcus Cohee
Marcus Cohee

Written by Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!

No responses yet