Give the Boss a fighting chance with turrets.

Esteban Ibarra
7 min readMay 15, 2021

--

Now that the boss’s sections get destroyed properly, I decided to start focusing on a bit of defense and start working on the turret prefab. Since I’m close to a deadline, I decided to use Unity's animator feature and just animate the turrets moving back and forth myself. It’s easy to do.

Turret piece in its current state.

I Put a turret sprite into the scene, added a BoxCollider2D to it, set it to trigger. Also added a target script so it can be destroyed as well as its own turret script. There’s also an audio source with a shooting sound effect: Loop and PlayOnAwake turned off.

First, to create the animation, open the animation window. If you don’t have it in your workspace, just go to Window/animation/animation:

Then press the red record button so that unity will record every change you make. Here are the steps:

at frame 0, rotate the turret a little but then in the inspector, reset it back to zero. You’ll have your first keyframe. Then go to one minute, rotate the turret to the extreme left.

At 2 minutes, rotate the turret to the center

3 minutes: rotate the turret to the extreme right.

at 4 minutes, copy the frame where the turret is at the center, frame 2, and then when your player line marker is at the 4 minute mark, press ctrl-v.

This will currently make the turret smoothly move back and forth, you may want to stop here and move on to the next section but I want the turrets to pause at each turn, so at 0:00, ctrl-c and go to 30 seconds and ctrl-v. Do this for every frame and you’ll have a more robotic turret! I’ve put an example gif, but I didn’t place the keyframes every minute, but I later fix it. Hopefully it gives you an idea on how easy it is to animate in unity though.

As it is now, the turrets all play at the same time, one solution will be to vary the speed at which they’re playing and I’ll do that programmatically in the following code.

Remember we have the target and turret code on this now. The target code hasn’t changed much:

The first change however is we’re now creating a holder for the collider and we’re also Instantiating explosions for when a target is hit and when it finally explodes. Also note that we’ve wrapped the regular target code inside a check to see if the collider is enabled. This is for our layer design. Again, the idea being that the layer above ‘protects’ the layer below until it’s destroyed. Once it is, the section code will turn on the colliders.

Resources.Load is a quick and dirty method of getting an item on the screen. Since this is the final boss battle and there will be no other screens, I think we’re OK in using it, and here a tiny explosion (the same one from the boom boom boom article) is used whenever a destructable item is hit by a laser.

A special folder for instant instantiation!

The resource folder is a special folder in unity that allows you to instantiate something on the fly using a path and not by the usual referencing methods. My understanding is it should be used sparingly and so having a couple of explosions at the ready fits the bill.

The next explosion is a larger explosion from our original enemy.

Here, we’re giving it a name, explosion, so we have a little more control over it once it’s instantiated and that’s going to be changing the explosions scale to match whatever the object is exploding. That way, if the player destroys a huge turret, you get a huge explosion and if you hit a tiny turret, you get a tiny explosion.

Before I forget, let me cover the turrets bullet!

turret bullet

This bullet has a boxcollider2D set to trigger and a rigidbody2d on it as well as a TurretBullet.cs script:

We have a bullet speed variable that’s set to -6 so it moves down on the y axis. I also created a getter and setter for this variable so that I can change it from the turret class to vary the speed.

In Start, we invoke destroy bullet after 5 seconds, the bullet will self-destroy and in Update, we just constantly move the bullet using transform.Translate. Nothing to it!

Finally let’s get to the turret code:

It seems to be a lot, but not really, let’s walk through it:

_bullet — holds the bullet object we’ll instantiate with every shot.

_leftGun and _rightGun are empties placed onto the turret for positional reference.

Empties shown as red diamonds — we’ll be placing our newly instantiated bullets at these positions.

Collider, audio, and animators are holders we’ll be using to control these components.

_speed was originally going to be used for programmatically animating the turret, now it will just control the animation playback speed.

_firerate is set at 3 seconds. This seems like a long time, but when you have over a dozen turrets firing at you, it still seems like much!

_canFire, we’ll be using this paired with _fireRate and Time.time. This keeps the turret from firing a steady stream of bullets.

the next 3 variables are to add a little of variance and make the actions feel a little more organic and less predictable.

_startTheCarnage is part of the _canFire/_fireRate duo. It will be used to stop the boss ship from firing for 7 seconds and let the player prepare.

_firstFire was created because the first shot from the boss was a huge deluge of inescapable bullets. This variable just fools the boss into thinking it fired all those bullets and then continues to fire bullets in a more scattered and fairer way.

the first four variables as you know are assigning holders to components.

I’m placing the ‘this’ in front of these variables to be sure that each individual turret has its own set of random numbers (I think that’s how it works).

And now we start the heart of the class with the Shoot Coroutine!

We wrap the entire function in an infinite while loop. This works because we’re always yielding and slowing the function down preventing unity from crashing.

Next we check to see if the collider is enabled. If it isn’t, that means we haven’t gotten to that layer yet and its waiting for the section code to enable them.

next is an actual call to fire, but since it’s the first one, we’re going to make it a dud and wait for the next one.

Now we add a bit of English to the bullet speed as well as the bullet speed spawning. we create a bit of time discrepancy from 0 to 1 and a half seconds and tack it on to the _canFire rate so firing isn’t too predictable.

We then instantiate two bullets called bulletLeft and bulletRight, notice that we use the _leftGun and _rightGun inside of the instantiation, this assures that the bullets will appear in front of each turret gun.

Finally, we use the bullet codes setter to add a bit of variance to the bullet speed so that isn’t too predictable either and gives the player a bit of challenge.

Now let’s see it in action!

--

--

Esteban Ibarra
Esteban Ibarra

Written by Esteban Ibarra

cartoonist, game artist, and wanabe gamedev.

No responses yet