Shooting Backwards!

Objective: if the enemy gets behind the player, add a function that allows them to shoot backwards.

We want to add another attack from the enemy, this time coming from below. If the time comes for the enemy to shoot and they are below the player we want to instantiate the lasers to go backwards, giving the enemy a chance to hit the player.

To do this we need to open the enemy script and look at the function that controls the enemy shooting.

This is the formula we must determine when the enemy shoots. We want to add a definition for if the enemy is higher than the player. So, if the enemy has a higher y axis value, then the player, then the enemy is higher than the player.

Now we need to add an else if, for when the player is higher, or has a higher y axis value. We will use the same formula; the only difference is the laser method we call.

Notice the red squiggle? Moving to the laser you’ll see the method we are calling is a private method meaning we don’t have access to it. Simply make this method public and the squiggles are gone.

Now our enemies will shoot back at the player if their time to fire happens when the enemy is below the player. As always, don’t be afraid to experiment with your code, and I’ll see you in the next tutorial!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store