In our next part of increasing the threat of our enemies, we are going to take a quick look at how we can have them target our powerups if they happen to be flying behind them. These enemies didn’t quite like the fact we can call the powerups to us, so they are taking it out of those powerups.
To start, we will make a detector similar to that of when we had them avoid our laser, but as this is just straight down, we will use a box collider over a polygon one:
From here, we will simply just add a new firing method to our enemy script so that when it passes by, in our new powerup script we have a command for it to follow:
From here, we will add a quick line of code for our detector in the new script:
Now, we can test to see if it fires in our editor by playing our game and seeing what happens when we drag a random powerup prefab in the line of view of our detector:
Finally, we need to make sure there is some interaction taking place between our laser and powerups. As like when our player or enemy gets hit by something, we need to create an event in which our laser and powerup get destroyed upon impact. We will also need to make sure that every powerup has the proper tag:
With all of this in place, we can test it in our game to see if it works as intended:
Now that we have our enemies firing at our powerups, we have now created a tough choice for our player. If we see the powerup, do we attempt to go for it if the enemy is firing at it at the risk of being hit? Or, do we use the collector but risk having a bunch of enemy lasers going across our screen?