The smart enemy gets even smarter!
So for now the smart enemy can avoid our laser, but could we make the enemy even more annoying? Well yes of course! By making it shoot backward!
Annoying enemy? Why not make it even more annoying?
We need an OnTriggerEnter2D for this one, an empty game object for the smart enemy, and move it behind the smart enemy. And once the player goes behind the enemy and triggers, the enemy will shoot a laser from its position which goes backward.
This is the script inside our PlayerCheck Game Object.
- We have the handle for our smart enemy.
- We get the enemy script from the smart enemy
- And when the OnTriggerEnter2D happens, we check if it’s the player and if it is, we use ShootBackwardsLaser();
So now I should create a laser that goes the opposite way than the normal laser which our enemy shoots.
I have my Laser Script which is assigned to the laser. There I have a boolean named _isBackwardLaser;
And because this is private, we can assign the laser which is in the smart enemy as _isBackwardLaser
This is the code that I have in the void Update()
First, we have an if statement to check if a laser is Player Laser, then else if, if the laser is a Backward Laser and the last one is a normal enemy laser.
We have this in our backward enemy laser prefab
So when we trigger this, the laser knows that it’s a Backward Laser and that’s how we get the laser to go in a different direction.
And now we have a laser that goes backward when our player triggers it! :)
Thank you for reading this and see you in the next one on the road to being a better C# Programmer! :)