Creating an Enemy that can Dodge your Projectiles

Frank Warman
Nerd For Tech
Published in
4 min readJun 2, 2021

--

Previously we added some Sine movement to some of our Enemies with a simple script to give them some more complex movement. Thus making it a little more difficult to destroy them.

Now what if we take it another step further? Creating an Enemy that has the ability to detect if there is a Laser coming it’s way, and attempt to dodge it? Well buckle up, cause it’s coming at you right now!

What do we say to death? Not today.

Creating the Enemy Prefab

For creating this Enemy, I figured that dodging a Player’s laser would feel a bit annoying to the Player. And wasps are annoying too. So I made this Enemy prefab smaller and tinted them a little yellow

Little dodgy, waspy, yellow enemy

And added the basics as with any Enemy Rigidbody 2D (gravity scale = 0), Box Collider 2D (Is Trigger selected).

Then created a DodgingEnemy script and attached it. And also attached our SineMovement script to it as well.

Inside the Script

This is where all the magic happens. It’s mostly a copy of our regular Enemy script — firing logic, downward movement, collision logic.

But now we’ll create some new Global variables:

Used to determine our dodging logic

Then our DodgeLasers() function:

In this logic, we’re utilizing a dynamic List of lasers (which will be created in a moment) that are handled by the Player.

We’ll be putting DodgeLasers() in our Enemies Update() function, so it will be constantly checking this List of Lasers, and if any are within it’s _safetyDistance, it will attempt to dodge either Left or Right.

List of Lasers

Inside of our Player script is where we will handle this List, since the Player determines when to fire a Laser.

We’ll create a public List of GameObjects called playerLasers:

Then, inside our FireLaser() we can add a created Laser to the list when it’s instantiated. It’s important to note that we need to create a reference to this new instantiation in order to add it to the List:

Disregard the Homing Missles for now…they’re coming soon ;)

To make sure this List is updating properly, we’ll go into Play mode and fire a few Lasers:

Our real-time dynamic list!

You’re wondering why some of the Lasers are disappearing… And that’s because I’ve added code to remove them from the List when they get out of bounds:

  • * Be sure to add the _player.playerLasers.Remove() code to other instances where a Laser gets destroyed, i.e. when a Laser destroys an Enemy!

Visualizing the Safety Distance

When tweaking your logic for the dodging enemy, it helps to visualize your changes and your distances.

This is where Gizmos are your friend!

Adding this to the end of your script will let you visualize variables:

And can also be seen during Play mode:

DODGED!

Success! We now have a little waspy Enemy that is quite the challenge to take down!

In the next article we’ll be covering that Homing Missile that snuck its way into our code in this article ;)

--

--

Frank Warman
Nerd For Tech

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles