Creating a ‘Smart’ Enemy.

In today’s article, we create an enemy that can shoot backward at the player if they get past them. I duplicated the base enemy and changed its appearance to look like there were guns on the tail of the ship. In our ‘Enemy’ script I created a new bool to distinguish if the enemy is the smart enemy.

Next, I created a new function that had the shooting logic inside. Most of it has been the same as what the other enemies have been using, with a few minor adjustments.

It’s a large function, but the main thing is that it checks the players’ y position against the enemy y position. If the player is past the enemy y position, then the projectiles will shoot backward. When testing this out shortly, you may run into the issue where the lasers are colliding with the enemy right away and destroying them. If you do, you will have to adjust the ‘laserOffset’ values.

You will also notice a new function from the ‘Laser’ script, called ‘AssignDoubleSidedLaser’. We will create that right now. You will need to create a new bool for the backward shot.

Create a new function in the ‘Laser’ script called ‘AssignDoubleSidedLaser’.

Now, we will edit our ‘Update’ to better accommodate the new enemy type.

That is all. Your enemy should be shooting in both directions now.