Shooting Backwards!

addam davis
2 min readAug 5, 2021

--

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!

--

--