Unity Dev Blog: Creating Enemy Explosions (and triggering them in code)
It’s about time…
Animating the enemy explosion is the easy part. Getting it to play at the right time takes a little more planning. Let’s sketch out the steps:
- Create the animation and attach it to the enemy game object.
- Trigger the animation to only play when the enemy gets destroyed during game play.
- Stop enemy motion / or remove the collision detection components so the exploding ship does not accidentally damage the player.
Let’s start by creating the animation itself. The assets I am using have individual frames of the animation saved as images.
With the enemy game object prefab selected, open the Animation window and create a new animation. Then drag in all of the images from the Assets folder in the Project window. You should be able to preview the animation.
Perfect! Now how do we get it to play only once time and only when the enemy ship is destroyed? First, make sure to turn off ‘Loop Time’ in the settings for the animation clip in the Inspector window. A looping explosion animation would clutter the playing field and is not really realistic after all.
Now comes the slightly more technical part; getting the animation to play on command when the enemy ship gets hit with a laser or collides with the player. If we run the game now, the animation will play automatically.
That is cool, but definitely not what we want to have happen. This is more like a Michael Bay movie than a video game…
The reason it is animating as soon as the game object is instantiated is because we never changed the default behavior. To do so, we need to tell the Animator controller for the animation to default to not playing the animation right away. We do this by creating a a new State, giving it a name, and setting it as the default.
Next we right-click on the new State and select Make Transition to create a transition from the EnemyDestroyed animation. Right clicking on this transition shows it has several settings that allow us to control when the animation plays, whether it waits to finish the current animation before transitioning, blending into the new animation, etc.
Since we want the explosion to happen the instant we trigger it in the code, I turned the ‘Has Exit Time’ off and set the Transition Duration to 0 seconds.
One last item we need to add within the Animator window before moving to the code. In order to trigger the animation to play, I created a Trigger parameter than can be controlled (triggered 😊) within the code in the script.
There are four types of parameters you can use.
- Float/Int: numerical (with or without decimals) that could be used to trigger the animation for a game object if, for example, its speed changes. In a future dev blog I plan to add animations to the player ship where it animates turning left or right. This could be done using a float value that corresponds to the rate of movement.
- Bool: a boolean value that is set to either true or false. For example, a boolean named isTheShipDamaged might be set to true which would trigger a smoke trail. Then repairs could be made and we’d set the isTheShipDamaged back to false, turning off the smoke trail animation.
- Trigger: the simplest type. It just triggers the animation to play. No fuss no muss.
In the screen shot above, I have already created a trigger parameter named ‘OnEnemyDeath’. I can now use this in the enemy script to trigger the animation to play when it dies.
Here it is in the code:
When the enemy gets hit with the laser, for example, a few things are going to happen.
- First we destroy the laser bolt game object. We do not need it any longer.
- We call the AddScore() function in the player script to add points for destroying the enemy.
- We stop the motion of the enemy ship/debris to keep it from continuing forward and possibly hitting the player. Remember, Unity treats it as a solid game object unless we tell it otherwise, and it keeps running all other code during this time. An alternative would be to turn off the collision detection while the animation is playing but before the game object is removed (destroyed).
- We call SetTrigger(“OnEnemyDeath”) in the animator. This triggers the explosion animation to play.
- Finally, we destroy the enemy game object. Notice the change from Destroy(this.gameObject) to Destroy(this.GameObject, 2.8f). The second parameter tells Unity to wait 2.8 seconds (roughly the length of the animation) before destroying the game object. Without this delay, the animation would not play because it is part of the enemy ship game object and gets removed along with the rest of the enemy ship.
So there you have it. The enemies now disappear in a fabulous explosion of fire and debris. As promised.
Join me next time where I will be adding damage animations to the players ship to visually display how damaged they actually are in real time. Smoke, fire, dragons… well.. not the dragons.
See you there!