2D Shooter: Enemy Attacks

Objective: Create a way for the enemies to shoot every 3–7 seconds.

First we need to create the prefab for the enemy lasers. Do this by dragging in the enemy prefab and two lasers into the enemy container in the hierarchy. Make sure that the local coordinates for all the objects line up, and position where you want the lasers to be. Then make an empty object, name it enemy laser, drag the two lasers into it, and then turn it into a prefab.

Assign this prefab to the enemy object through making a variable in the script, then instantiate it with a fire rate similar to what we did here and here.

Now its time to get the laser to move down instead of up. Go to the laser script and copy the movement code into two separate methods. In the second one you’ll need to make some changes, like changing ups to down, and changing a couple values.

Now its time to call these in the update method using an if statement.

Make a method that will change _isEnemyLaser to true when called.

Now go back to the enemy script and create an array for the lasers, and an for loop so they can be assigned as enemy lasers.

Now go to the laser script again and lets make a collision method so the lasers can detect when they collide with the player.

Now we should have dangerous enemies!

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!