Enemy Dodging Player Lasers

Ali Emre Onur
3 min readApr 5, 2021

Today, I have completed the full 3rd week of my journey. The game is almost finished, but I will be working on some minor details to make the game perfect.

I am actually pleased about the progress I have made after 3 weeks. I am feeling more comfortable than ever. Playing a game of myself, and trying to optimize it actually gave me more joy than I have imagined. I am really loving this!

So for the daily report:

What I have done:

  • Player will encounter a boss after finishing the 4th wave.
  • Enemies can dodge some lasers.
  • Enemies can shoot the power-ups if they are on their shooting way.
  • An enemy type can shoot you backwards if you somehow get above it — Protect your back!
  • Made a couple of optimizations for a more competitive gameplay.

Things I am struggling with:

  • Boss is still not the way I have imagined. I am planning a unique shooting style and ulti at critical health levels rather than standard shots.

Win of the Day: Dodging Lasers!

Aside from getting more familiar than ever with the Raycasting (this topic really deserves an article devoted only to Raycasting), my struggle with the dodging behavior was another win of the day.

I thought it was a simple thing, but really had my time to solve it. Certainly, there may be many ways to solve the problem but here is how I solved it:

First, I have created an empty child object in the Enemy Prefab. Then, I have added a BoxCollider2D to the child object, and arranged it’s size&position according to the way I want it to detect the incoming lasers.

As you can see above, enemy’s own collider is on it and the collider for detection is just in front of it.

I have created a Detector script and added to the child object. Then, I have added EvadeShot and EvadeOver methods to the Enemy script.

The idea is simple, enemy should change it’s default movement once the “Laser” game object enters the collider, and continue to it’s default movement after it exits the collider.

Laser Detecter Script

Just as I have thought this has been too easy, bum!, enemies got destroyed before the laser even touched them. The new, child collider was working on the parent object like its own.

So what’s the problem? How can I make the parent and the child collider work without interfering each other?

Well, after some other trial-errors, I have decided to search for this and there was the solution. I have added a Rigidbody2D on the LaserDetecter and changed it’s body type to “Kinematic”.

Since you wouldn’t want all of the enemies to dodge your bullets, make sure to design it in a smooth way. I really loved while balancing it.

--

--