Nerd For Tech
Published in

Nerd For Tech

Creating Enemy Explosions

What more satisfying way of knowing your enemy has been destroyed then adding an explosion effect to it all. Right now, all that happens is the enemy disappears from the screen upon death, but now we will look at adding an explosion effect that we have onto the enemy. First, let’s create the animation for the explosion effect and see what happens when we play the game:

Now let’s see what happens once we play our game:

As we can see, all that is happening is that our enemies explode upon entering the game and the effect loops over and over. Visually, looks exciting, but practically pretty useless. So, how do we go about fixing this issue? First, we will change the loop off, along with creating an empty connection within our animation animator:

Now that we have created a new state, let’s connect it to our destroy animation and see how it looks in game:

Now there is a slight delay, but it still doesn’t do what we want. The reason is because we need to set a parameter for our animation to activate from:

As we can see from above, there are 4 types of parameters we can set it for, which are the basic 4 variables we work with being Int, Float, Bool and Trigger. In this instance, we want to choose the trigger. We can test it in our game and manually trigger it to see what happens after applying it to the transition line in our animator.

Now that we know that the trigger parameter works, we have to create a code that will initiate the trigger so that it works when our laser hits the target. If we search it up in the Unity script API, we can see a various amount of methods that can be used for the animator. What we are looking for is the SetTrigger method, and the code that we need to use for it:

Now that we know what we need to look up and have an idea what the code should look like, let’s go to our script and write out some pseudo-code in the locations we need to build our code in and work from there:

As we can see, we need to create a handle to our anim, assign the component and then trigger the animation both with the player and laser upon impact. Once we have done all of this, it should look something like this:

Now that we have this all entered, let’s test it out in our game and see how it goes:

So, as we can see, the trigger parameter works, but there are a couple issues. First, the enemy keeps moving after being destroyed and even if we shoot it, the trailing explosion results in us taking damage on our player. As well, there is a delay upon the time that the enemy is destroyed and the explosion occurring.
First, lets begin with the period of time that the enemy takes to be destroyed. If we go over to our animator on an active enemy unit, we can see that the new state we created has a bar cycling a load time:

What this period of time indicates is how long it takes from continuing onto the next step, or the exit time. We can adjust it to whatever we wish, but in this case, we will just simply turn off the exit time so that all that needs to happen is the trigger event:

Now, how do we prevent the player from taking damage from the flying explosion through space. Well, we could simply say it’s a design of the game and leave it at that, but it could make it pretty frustrating for our player. So, what we can simply do is in our code, change the speed of the enemy to 0 when it explodes and it will instead just stay in place:

Now that we have adjusted the code, let’s see our results in the game:

And there we have it. We have now gone from having our enemies simply disappear from our screen when destroyed to adding the animation of it exploding into pieces. From here, we can look into adding and obstacle for our player to avoid, along with creating some animations to add onto our player so it looks a bit more interesting.

--

--

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