Creating Enemy Explosions
Let’s Have a Blast
The current action of my enemy deaths was set to only Destroy the enemy game object. While this is exactly what I wanted to happen, now I want to see an explosion animation when I destroy it.
- I selected the enemy prefab in the Projects panel and opened the enemy prefab for a dedicated view.
What I was looking to do was to add the explosion animation onto the prefab.
- With my Animation panel open I selected Enemy from the Hierarchy panel, created a new animation named, “Enemy_Destroyed_anim”, dragged the explosion animation sequence files into the Animation panel, and ran the animation see it in action. Wow! What an awesome explosion animation!
The only problem at this point, was the animation played at the start of the game without actually having to shoot the enemies. Also, the explosion was in a loop.
- The first step I took was to turn off “Loop Time” in the animation by unchecking it’s corresponding checkbox in the Inspector panel.
This obviously turned off the looping, but now I had to stop the explosion from executing immediately upon instantiating the Enemy game object.
- I opened the controller for the “Enemy_Destroyed_anim” animation. The controller was named “Enemy”, but to be consistent, I renamed it to “Enemy_Destroyed”.
- Inside the Animator window, I created an Empty State. I renamed it “Empty”. I then set it as Layer Default State. This stopped the animation from running immediately at Enemy instantiation.
Okay. That did the trick. The animator now executes the Empty State, which is empty, so the explosion animation no longer runs on start.
But what about when I destroy the enemy? I need to run the animation at that point.
- Going back to the Animator panel, I made a transition from Empty to Enemy_Destroyed_anim.
What this does is runs the Empty animation and then runs the Enemy_Destroyed_anim animation. Obviously, I didn’t want the explosion animation to be run immediately.
- I went into the Animator panel and had to add a Trigger parameter to let Unity know when I want to move from the Empty State to the Enemy_Destroyed_anim State.
- I named the Trigger parameter “OnEnemyDeath”.
- By clicking on the transition arrow in the Animator panel I went to the Inspector panel, under the Conditions section, and added the “OnEnemyDeath” condition.
Writing the Code
- Inside the “Enemy” script, I created a handle to the Animator component that’s attached to the Enemy game object.
- Inside the Start() method, I then set “_anim” to the Animator component. As a best practice, I added a NULL check.
Triggering the Animation
- To trigger the animation, I added code SetTrigger() code for the “OnEnemyDeath” trigger parameter I set in Unity. I had to add this for when “Enemy” is destroyed by both the “Player” and the “Laser”. To avoid the object being destroyed before the animation can play, I also added a time lapse to allow the animation to complete before the object was destroyed.
I noticed after hitting an enemy with the player or a laser, the collider on the enemy could still hit the player, causing the player to lose a life. I had change that.
I decided the most optimal way to do this was to use GetComponent to get a hold of the Collider attached to Enemy.
- I first added the following code to my “Enemy” script.
private Collider2D _enemyCollider;
- I then had to get the collider component, assign it to “_enemyCollider”, and null check the component. I added the following code to my “Enemy” script, inside the Start() method to do this.
_enemyCollider = GetComponent<Collider2D>();
if (_enemyCollider == null)
Debug.LogError(“The Collider2D is NULL.”);
- Next, all I needed to do was to turn of the collider. I did this by adding the following code in the “Enemy” script. I had to add it for both the Enemy and Laser.
_enemyCollider.enabled = false;
And there I had it. After shooting or hitting an enemy, the destroyed enemy was no longer able to collide with the player.