Evasive Action: Dodging Enemies!
Recently added some evasive enemy behaviour for the ‘Smart’ enemy types in this 2D Space Shooter. I opted for using the raycasting system I previously implemented, having the enemies now detect whether the collider their raycasts have hit has a ‘PlayerLaser’ tag. If so, I’m calling a Dodge coroutine followed by a RaycastHitColldown coroutine. These methods both have some booleans associated with them, just to ensure there’s an approriate cooldown on the raycasting and that they aren’t simultaneously dodging and moving as normal.
As for the Dodge coroutine specifically, it’ll move the enemy back and to either the left or to the right, making use of a while loop and Vector3 lerp to do so.
One of the main drawbacks of this approach is just that the player’s lasers need a fairly generous collider in order for them to, realistically, ever get detected. But for me, it made sense to make use of the pre-existing raycast method in order to detect incoming attacks 😉