Nerd For Tech
Published in

Nerd For Tech

“Smart” Enemy that shoots behind itself

We’re in the stage of making our Game more difficult, so why not another Enemy type to throw the Player off! This Enemy will be given the ability to shoot behind itself, making the Player watch their back once they’ve dodged their forward attack!

This is actually a simple process that only requires a few tweaks in our already existing code (if you’ve been following these articles).

Watch your six!

Creating the Enemy

I’ve dubbed this Enemy the Double-Sided Enemy because that’s exactly what it is — two enemy sprites, one facing forwards and the other backwards!

Double-Sided Enemy preview

In creating this enemy I also had to adjust the Enemy Shield proportions to fit it’s oblong size.

I added the two sprites to a Parent object, layered them and tinted them. Gave the Parent a Rigidbody 2D (gravity scale set to 0), a Box Collider 2D (Is Trigger selected), and created a DoubleSidedEnemy script which is almost a copy of our regular Enemy script, with a few exceptions.

The change in the code is within the Firing logic:

Basically if the Player’s Y position is below the Double-Sided Enemy’s Y position, it shoots a normal Enemy Laser. But if the Player is above the Enemy’s Y Position, then it will shoot an Enemy Laser and assign it as a DoubleSidedLaser, making it go upwards!

The Laser Logic

Creating the logic change in the Laser script needs only another variable, and an updated Movement logic.

The variable we need to create is a bool _isDoubleSidedLaser:

And to assign the Laser we’ll create a function AssignDoubleSidedLaser():

Then to update the Movement logic:

Which now requires these two functions:

Now we have an Enemy that can shoot at the Player when they are least expecting it! This should make the game more challenging and force the Player to be more aware of each Enemy type and their surroundings!

With all these new Enemies and challenges for the Player, let’s throw them a bone in the next article! We’ll create a simple magnetization key that will cause the Powerups to gravitate towards the Player!

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Medicine & software engineering

Playing with Docker 1.13 & Minio

Bunzz rewards your deployed DApp with 300 USDC 💲

Cloud Agnostic Microfrontends and Microservices Architecture

By Request

【Python】Implementation Of Logistic Regression

SamoDEX launched

Enabling the Next Generation of Robotics: Our Portfolio Company Freedom Robotics Is Hiring

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
Frank Warman

Frank Warman

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

More from Medium

Embedded System

A quick guide to player setup in Unity

ESP32 Project 5: Output-Display & PWM

Setup ESP8266EX for C-programming with no VMs (or Arduino) on Windows 10