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

How to Upgrade OpenCart 2 to 3. A Detailed Guideline

Java, JVM, JRE and JDK

Really Dumb, yet Valuable, Tests

7.2 AsyncTask and AsyncTaskLoader

Ship in 30 days

Building better student tracking in rural areas

Learn SQL — Intermediate

Refactor Laravel route file

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

Week 11 — Open Source [2021/12/27]

NgRx vs Observable Services: Stately Matters

top down view of a person’s shoes pointing forward with two arrows pointing to different paths to take

JavaScript to Xamarin.Forms Two Way Communication Setup

How To Fix “ng Is Not Recognized As An Internal Or External Command”?