Creating New Enemy Types: The Porcupine Blowfish Part 4 — Finishing Touches

For this short article, my objective will be to finish up my new puffer fish enemy type by giving it a firing routine and some animation.

In a previous article, I covered making the first and last frames of animation for the porcupine puffer.

I admit that in hindsight, creating these additional frames to animate this little critter took longer than it might have been worth. Rotating the spines by hand into foreshortened perspective was a bit tedious, but I am happy with the final result.

The next step is exporting PNG files to bring into Unity as Sprites. I use the sprites to create a new animation for the puffer fish, also adding in the capsule collider 2D to animate the size.

Here is a clip of the puffer animation in a loop!

I only want this animation to happen when the puffer fires it’s spines out, so I open the Animator and add a default state before the animation state. The default state has a trigger parameter of IsPuffing added to it, which will be used to trigger the animation via script. Also to note it that loop time is turned off at this point.

I click on the transition moving from the default state into the animation state and add the condition that the IsPuffing trigger needs to be called. Has exit time is turned off to make for an immediate transition into the animation when called. Another transition with no conditions is added to move back to the default state after the animation has completed.

Now I need to reference my IsPuffing trigger in the enemy class. The first thing I need is a reference to my Animator.

In void start, I use GetComponent to grab the Animator off the enemy blowfish. I then null check the Animator as well as Start the coroutine handling the firing mechanism (more on that shortly).

Here is the coroutine that starts when the blowfish enters the game. I give a 2 second delay before entering the while loop that handles the firing mechanism. I will at some point randomize this value between a range of seconds for more variety.

The while loop will run for as long as the isEnemyDead bool is set to false (this is set to true on enemy death). I first use the animator variable to call the animator and trigger the IsPuffing animation. I add a time delay of 0.1 seconds to give the puffer time to expand before firing projectiles. The firePoint variable offsets the projectile instantiation to fix some visual lag due to the enemy movement. The firing mechanism is then triggered and another time delay is added of 5 seconds before beginning the loop again. More on how the firing mechanism works here.

If you are curious about how the enemy movement works, you can read about that here.

That’s a wrap for my new enemy type. I am excited to move on to some other features, so thanks for reading and I hope to see you in my next article!

I am an artist and musician, that is currently diving headfirst into game development with C# and Unity3D.