Phase 2: Enemies Avoiding the Laser

Object: Create an Enemy that can avoid being hit by the Player’s laser.

First I will create a new Enemy and call it Enemy_Dodge. In the new script I will inherit the Enemy class, this will allow me to reuse the enemy script

I will use RayCasthit2D to detect the laser. First I need some variables for the cast radius and length. Also a variable for the dodge rate.

The last thing I will need to do is create a layer for the laser that the raycast will check for.

In the update method, I will add the RayCastHit2D

When a laser is detected the enemy will move to the left by the dodge rate. Also, the dodge rate will be decreased by .3f until it's below 0 then it's set to .05f.

Let’s see this in action

Happy Coding!!!



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