Boss Fight: Arming the front guns

I’ve almost forgotten the next layer of weaponry to our front weapons so let’s give them a fighting chance!

FrontGunnerBullet.png

For the front guns, the bullets will be these ‘smokerings’ that are shot out towards the player. one isn’t impressive but since we begin with 5 or 6 of these weapons, the circles tend to create a wall that can be hard to escape so it’s best to destroy them as soon as one can!

As with any other sprite, after importing the image into Unity, change it to a 2D Sprite. You may want to animate it growing bigger. It’s easy to do, just open the animation window, create a new animation and push the record button, set the circle at .10 on the first frame and full size on the last frame at 30 seconds. Then go into the Animator window, double click on the animation and turn off looping. We only want it to grow once and stay big.

Don’t forget to add a box or circle collider2d and rigidbody set to gravity 0!

We’ll also create a new script for this bullet called FrontGunBullet:

We want this bullet to be slow and imposing so we’ll give it a speed of -2f (-2 because we’re shooting down initially)

In Start, we’ll use an Invoke to run a function to destroy the bullet after 4 seconds.

Then we have a Vector3 called shootDir. We’ll be using some usual Pythagorean math to figure out the angle to shoot between our turret and player in the frontgun code.

This same shootdir will be passed into this code in the Shoot command. We’re just going to copy the Vector3 into our local variable.

In the update, our bullet will just float in the direction where shootdir is telling us multiplied by speed and Time.deltaTime as you know will make sure we get the same movement no matter what machine is running our game. For a while, the bullets were floating in the opposite direction of the player, turns out I had to subtract the direction from our current transform position in Update.

Let’s take an overview of the frontguns code:

Seems like a lot, but as with previous experience, it’s pretty straightforward, We’ve actually gone through similar code in the past so I’ll just give a quick overview.

First we place a holder for the laser ring and call it _laserPrefab to be original.

Place a holder for the collider and then assign some variables for shooting the laser among a short time period.

_target and _shootPosition are the positions for our Pythagorean math.

We place the proper objects in their holders in the Start function.

In Update(), we first check to see if the collider is activated. If it isn’t, nothing is done. The gun can’t shoot, but it can’t be hurt either. Once activated though, it begins its routine and checks the time against CanFire. once it can, it creates a time variance for the next shot, creates the angle with the shootDir variable where it subtracts the players position from the turret and then passes that into the instantiation through GetComponent.

Finally _canFire is reset and the fire rate and variance is added to the _canFire variable.

The bullets work perfectly! We’ll need to put a check in the player for when they hit, but that’s easy enough:

As I made the player a static instance, it’s really easy to access its Damage() function which will remove 1hp.

As a challenge, I invite you to do the same for the turret bullets!

That’s all for today, Tomorrow we’ll give our boss a proper sendoff!

cartoonist, game artist, and wanabe gamedev.