“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).
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!
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!