2D Game Dev Journey — Day 21: Enemies That Dodge

Welcome to 2D Game Dev Journey — Day 21: Enemies That Dodge. Today I go over the new enemy type whose main goal is to simply dodge the player’s lasers.

Starting Point & Setup

Today we will be starting right where we left off in 2D Game Dev Journey — Day 20: New Background & Negative Power-ups.

To get set up for today, I went ahead and created the enemy design for this dodging enemy type. I figured it would have to look pretty sleek and nimble, and with that in mind, I created the following sprite below:

Putting on the Moves

With our new sprite, we can now go about writing in the code necessary to get them properly dodging our player. Now there are a few ways one can go about accomplishing this, but I decided to make use of object inheritance and polymorphism, and raycasts (specifically 2D circle cast for our purposes). I am using object inheritance and polymorphism because we already have an enemy script that has much of the behavior we want and need. The only real difference between the basic enemy type and this enemy is the fact that this enemy will check to see if a laser is in front of it and if so, it will dodge out of the way.

Example of polymorphism

Considering that, why re-invent the wheel? Let’s just make some improvements for what we need instead. After some coding, my code for the enemy dodger simply ended up being one function (now that’s what I call “CLEAN”). Here is the how enemy looks in-game:

Conclusion

Thanks for checking in and reading today’s article. Tomorrow I will be creating a “smart” enemy type. If I have made any mistakes, please let me know so I can correct them for future readers. If you would like a more in-depth tutorial about anything I went over today, let me know and I will take the time to write something up.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store