120 Days to Master Unity (Day 9)

Brian Perry
5 min readMay 18, 2021

Creating Enemy Explosions

Today I will explain how to create an explosion when an enemy is hit.

The first thing that needs to happen is to create an explosion animation. I used a database to get my animation to play for the enemy vessel exploding. Next, open the enemy prefab by double clicking it. It will bring you to the enemy prefab view.

After this, an animation has to be created for the explosion on the enemy prefab. Open the animation window, if it is not already open, and click “create”. This will open a “dope sheet” for this prefab. With the dope sheet open, simply select all of the frames of the exploding enemy animation and drag them onto the dope sheet. This will create an explosion animation during the game.

As you could possibly imagine, this is not all that has to be done. If you run this like it is, as a matter of fact, it will not really work. It would, in fact, just keep playing all of the spawned in a continual loop of exploding.

This is probably not the effect that is wanted. This is where the animation controller comes in handy. Fortunately, the animation controller is created automatically when the animation is added to the dope sheet. Also, all prefabs, have a built in “Component” for animation control. The first thing to do is to modify the animation controller so that it is only playing after the enemy ship is destroyed. To modify the animation controller, double click it. It will open the animator window.

Animator State Machine

In the animator windows, you will notice that there is an entry box and a box that has the name of the explosion animation in it. This setup is the reason the ship is playing through an infinite loop of exploding over and over while the game is playing. There has to be a way to use this controller to make this animation only play after the player shoots it or after it collides with the player. One way involves a combination of using the Animator component in the enemy prefab, using this controller for the explosion animation, and modifying the enemy script. The first thing to do is to modify the animation controller so that its not continually looping exploding enemies. This can be done by right clicking on the animator window where the other state blocks are and selecting an empty state. Once this is done, give it a good name like “OnEnemyExplode” or something like that. After you have this block, you can put it between the entry block and animation block so your enemies are not just cycling through the explosion animation constantly. Using this block, you can now add a “trigger” it so that you can trigger it from the script. To add a trigger, click on parameters and click the plus button to create a new parameter. This will open a menu. Select on the trigger. Give it a name like “OnEnemyDestroy” or something similar. You now have a trigger animation for you explosion.

Now we need to go into the inspector for this new block. We have to add a condition for this new block so that its only going to play the animation when it is triggered by our script. Click on transitions. This will show the conditions. Click on conditions and click on the new trigger name you just created. Now your animation will not play unless this trigger is called from the script.

The next part of to get this working right is creating the script to get this animation to play at the right times. The first thing to do in the script is to get a handle to the animator inside of the enemy prefab script to call the animation. Since we are going to be handling when the enemies explode from the enemy script, we just need to get the animator component. To do this, we must first make an Animator datatype. I called mine explosion.

Next we have to get an instance of this component. This can be done in start() and using GetComponent<Animator>(). Now we have a handle to the animator component in our enemy prefab object.

To finally “trigger” the explosion, all we need to do now is go into the part of our script when the enemy explodes and use our explode reference to the enemy object to use “SetTrigger()”. We can just call the trigger directly like explode.SetTrigger(“OnEnemyExplode”). This will trip our trigger and make the explosion happen on the screen.

Our explosions should now be working but there is a problem because now the enemy ships are just disappearing like they were before with no explosion. Checking the script, we see that even though we scripted the explosion, we destroy the enemy object right away which cuts off the explosion immediately without giving the animation time to play.

Now our explosions seem to be working. But now their seems to be another weird problem. For some reason, there seems to be some kind of delay between when we hit the enemy and when the enemy actually explodes. This can be easily fixed by clicking on the Animator and the enemy destroyed state box and unchecking “Has Exit Time”. Also make Transition Duration and Transition Offset into zero.

After all this is done, when running the game again, the explosions should happen right away instead of waiting.

--

--