With one exception, all enemies follow a linear vertical path. This movement makes it simple for the Player to shoot them down accurately. I want to introduce another level of difficulty.
Allow enemies to dodge the Player’s attacks.
How I went about it:
This challenge boils down to having the enemy detect the Player’s bullet and move to avoid it. The first thing that comes to mind is a RayCast2D. However, with this approach, the area of detection would be limited. I can therefore take advantage of another Physics2D method.
As the name suggests, BoxCast uses a rectangle rather than a Ray. For more information, please refer to the Unity Manual.
I can use this method for detecting the Player’s bullet more accurately. I, therefore, started by creating a new Script for the enemies.
First, I declare all the variables that I will need to make this work.
Next, I create a method that returns a bool to check for a bullet.
The next step was to create a method that would check for incoming bullets. This new method will be called every frame in Update.
Finally, if the enemy detects a bullet, it will begin to move laterally. To achieve this, I wrote a new Coroutine.
Once the enemy reaches its set destination, the Coroutine will end, and it will continue to move down.
Now we have a game! These enemies will be harder to hit and will put the Player’s ability to the test. Just look at them move!
After adding this new feature, the gameplay is starting to feel more dynamic. Adding levels of complexity to the game mechanics sure makes for a fun experience. If I find this dodging feature too powerful, I can always add a random value to it. However, for now, I think it will work just fine.