The smart enemy gets even smarter!

So for now the smart enemy can avoid our laser, but could we make the enemy even more annoying? Well yes of course! By making it shoot backward!

Annoying enemy? Why not make it even more annoying?

We need an OnTriggerEnter2D for this one, an empty game object for the smart enemy, and move it behind the smart enemy. And once the player goes behind the enemy and triggers, the enemy will shoot a laser from its position which goes backward.

Player Check Script

This is the script inside our PlayerCheck Game Object.

  • We have the handle for our smart enemy.
  • We get the enemy script from the smart enemy
  • And when the OnTriggerEnter2D happens, we check if it’s the player and if it is, we use ShootBackwardsLaser();

ShootBackwardsLaser Script

So now I should create a laser that goes the opposite way than the normal laser which our enemy shoots.

Laser Script

I have my Laser Script which is assigned to the laser. There I have a boolean named _isBackwardLaser;

And because this is private, we can assign the laser which is in the smart enemy as _isBackwardLaser

This is the code that I have in the void Update()

First, we have an if statement to check if a laser is Player Laser, then else if, if the laser is a Backward Laser and the last one is a normal enemy laser.

We have this in our backward enemy laser prefab

So when we trigger this, the laser knows that it’s a Backward Laser and that’s how we get the laser to go in a different direction.

And now we have a laser that goes backward when our player triggers it! :)

Thank you for reading this and see you in the next one on the road to being a better C# Programmer! :)




From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why cloud hosting is better than shared hosting?

Basics of Flexbox — 2

We launched our $JELLY airdrop and it is just the beginning!


Web Technology

ROI Formula: A Detailed Guide on ROI Calculation

GameDevHQ — Day 18 — Enemy Movement Patterns and Negative Pickups

Coding Bootcamp : Why it is still the future

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
Janika Suhonen

Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

More from Medium

Variables! — The building blocks of programming

Destroying objects!!!

Improving Upon Selection Highlighting in Our Game — Part 1

5 Common Unity Game Development Mistakes