Creating a smart enemy which can evade players laser
While thinking that I need a challenge in this game, after creating that challenge… I think that why did I do it? It’s annoying! :D
This smart enemy can dodge my laser and even shoot backward. And when you forget that and more enemies are spawning, you are in big trouble.
Like in the article about the enemy which shoots the power-up pick-up. We use OnTriggerEnter2D in this one too and an Empty Game Object.
Smart Enemy which avoids Player Laser
In the empty game object, we have the same Laser Check script which we had in the article about enemy shooting power-up.
And the AvoidLaser(); method looks like this which makes our enemy move side for few seconds and then it returns to its original position.
- We have a boolean hasDodged to determine that the enemy can’t dodge infinitely. And if the boolean is false, meaning the smart enemy hasn’t dodged yet.
- We change the boolean to true, then we use transform.Translate to move our smart enemy a specific amount on the x-axis.
For that, we have created a private float avoidAmount
- And in the end, we start a coroutine that determines how long the smart enemy is in a new position before going back to the original position.
This is only the first part, in the next part I will write how I made the enemy shoot backward.
Thank you for reading and hope this helps in any way on your road to being an even better C# Coder! See you in the next one!