VFX — Adding Enemy Explosions

Who wants a video game without visual effects?

Let’s open our Enemy prefab in prefab view, create an animation, and load it up with our explosion sprites.

Now, we have a working animation, but we aren’t controlling it.

Hit play and enemies will constantly be exploding for no reason…

We’ll start to tackle this issue by first disabling “Loop Time” on our Enemy_Exploding_anim animation.

On our animation controller, in the “Animator” window, we’ll add an empty state and make that default.

Enemies spawning now won’t automatically automate into an explosion.

We’ll need trigger that in code, but first we give ourselves a parameter to do so.

Click “Paramters”, the plus sign, and then “Trigger” type and name it OnEnemyDeath.

Now, let’s add a transition line from our empty state to our exploding animation state.

Click the transition line and add a condition, which will default to our only parameter.

Unity Scripting API for Animations

Before we continue, let’s dive into the Unity documentation before we attempt to implement our animation in code.

“Interface to control the Mecanim animation system.”

“Sets the value of the given trigger parameter.

This method allows you to set (i.e. activate) an animation trigger, to cause a change in flow in the state machine of an animator controller.”

Now that we have some background provided by Unity’s documentation, let’s dive in.

We’ll start by adding an Animator variable, _enemyAnimator, to track our Animator component on the Enemy game object prefab.

This Enemy script resided on the Enemy gameobject prefab, so a simple “this.GetComponent<>()” gets us what we need.

Let’s go ahead and use best practices to add a null check to this component in our Start() method.

Since our death sequence is getting (slightly) more complex and we call it for more than 1 event, let’s go ahead and create a method just for OnDeath() of our enemy game object.

In our OnTriggerEnter2D() method, we replace any “Destroy()” code with this new method.

Let’s go over the code we’ve added to the OnDeath() method.

First, we use our new Animator variable to trigger the exploding enemy animation.

We know there will be a delay between the enemy being killed and the game object disappearing in the game while this animation plays out.

Since we don’t want the player to be damageable or killable during this time by this dead enemy game object, we disable the boxcollider component of the enemy game object.

If we look at our enemy exploding animation we can see the relative length of it and set a delay to our Destroy() call of approximately 2.5 seconds.

If we run our game, we see that the enemy’s explosion animation has a bit of a delay before it starts.

We want to select our transition line in the Animator window and set “Has Exit Time” to off so that the animation fires off immediately.

Lastly, our enemies begin exploding but reach the bottom of the screen and proceed to respawn at the top of the screen, finishing the explosion animation.

This looks all wrong!

Let’s add a Boolean variable to track when our enemy game object is in a “dying” state.

Let’s set the _isDying variable to true when our OnDeath() method is called.

We know the code that respawn enemy game objects that didn’t die back to the top of the player screen is in our update method.

So we track that down and add a check for _isDying == false before respawning the enemy game object.

Give it a run.

Next time we’ll add asteroids to our game!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Autonomous Technology Needs Better Simulation

The Cost of Poor Contract Management in Revenue & Sales

Yes I’m Changing

Speed up Docker Android Build

Comparison of throughput between Amazon RDS and Google Cloud SQL

System design reading: Spanner

DevFest Italy 2020, a WORLDWIDE event

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
GameDev Dustin

GameDev Dustin

More from Medium

Adding lighting to our URP scene

Draggable UI In Unity

Setting up Git for Unity

Image Courtesy of ARreverie Technology

P4: Creating a Gritty Office Den