This article is Part 16 in a 25-Part Series.

This will be a short post as it is more about the animation than anything else. You will need a explosion sprite for effects.

I will be doing animation by manipulating of transform.

Start by dragging your EnemyTank and PlayerTank prefab to the hierarchy. Then add your Explosion Sprite as a child to the PlayerTank and EnemyTank. Call it Explosion. Reset its transform.

You might need to adjust its scale to fit the explosion to be smaller than your tank. The animation will expand the scale to create the explosion effect

.

Then disable the Explosion Game Object for both the PlayerTank and EnemyTank.

Now let’s do animation for the Explosion. Select your PlayerTank in the hierarchy.

Open the Animation Window via the Unity Editor Menu(Window -> Animation). You should be seeing the TankCreating Animation in the Animation Window. Click on the dropdown Create New Clip... showing at the Animation Window. Unity will prompt for a name for the animation, call it TankExploding. Now you should be able to Add Property from the Animation Window.

  • Add 6 properties Explosion->Is Active, Explosion->Transform->Scale, Enemy AI->Enabled, Player->Enabled, Box Collider2D->Enabled and Body->Sprite Renderer->Enabled from the Animation Window. Once added, you should be able to see something like the below.

  • Now click on the red circle(symbolizing record). If it is depressed, means we are in recording mode. Check the box for Explosion: Game Object.Is Active. Uncheck the box for EnemyAI.Enabled, Player.Enabled, Box Collider2D.Enabled and Body Sprite Renderer.Enabled.

  • Click on the timeline at point 0:10, you should have a white line that will move to that position. Once the white line is at point 0:10, Set value of Scale.x and Scale.y to 1.25 and ensure the rest of the settings remains unchanged as compared to the first timeline change.

  • Click on the timeline at point 0:20, you should have a white line that will move to that position. Once the white line is at point 0:20, Set value of Scale.x and Scale.y to 1.5 and ensure the rest of the settings remains unchanged as compared to the first timeline change.

  • Click on the timeline at point 0:25, you should have a white line that will move to that position. Once the white line is at point 0:25, uncheck the Explosion Game Object is Active and ensure the rest of the settings remains unchanged as compared to the first timeline change. Click on the Add Event button. A white ribbon(turns blue when selected) will appear just below the 0:25 timeline. And the inspector should come up with the title “Animation Event”. Select from the drop-down Death().

Right-click on one of the rhombus at the point 1:00 and click Delete Key. We do not need that.

  • Click on the red circle symbolizing record to stop recording. Now your explosion is completed. Click on Play of the Animation to start enjoying being bomberman.
You will not see the tank as its sprite renderer is set to inactive at the start of animation.

The next step is to add the TankExploding Animation as a state in the Animator. If you go to the Animator Window, you will realized the TankExploding state is already added to the Base Layer.

From the Animator Window, go to the Parameters tab and create a trigger named killed.

From the Animator Window, right click on the TankCreating box and select Make Transition. Drag the arrow to the TankExploding box. After the boxes are joined together by the arrow, click on the arrow and uncheck Has Exit Time and add condition killed to it. Do the same for Normal box to TankExploding Box.

Explosion Animation for Enemy Tank

Now we can work on the part for Enemy Tank. Create a new Animator Controller called EnemyTank. Select your EnemyTank and add an Animator Component (Miscellaneous -> Animator). From the Animator Component, set the Controller to point to the EnemyTank Animator Controller you created earlier.

Go to the Animator Window, right-click on the Animator Window Base Layer and select Create State -> Empty. Rename the New State as Normal. Drag and drop the TankExploding Animation to the Base Layer. Then go to the Parameters tab and create a trigger named killed. From the Animator Window, right click on the Normal box and select Make Transition. Drag the arrow to the TankExploding box. After the boxes are joined together by the arrow, click on the arrow and uncheck Has Exit Time and add condition killed to it. Do the same for Normal box to TankExploding Box.

Some lines of Code required

Creating this explosion requires some little tweaks to be updated in the Health script.

First we need to declare 2 variables at the beginning. A Animator called Anime and Rigidbody2D rb2d. At the Start Monobehaviour, we will get the reference to the Animator and Rigidbody2d of the tank so that we can set the trigger killed. to start the explosion and rigidbody to stop the tank for moving once it has exploded(if not we will see moving explosion)

Animator anime;
Rigidbody2D rb2d;
//earlier codes are omitted for focus
private void Start()
{
	//earlier codes are omitted for focus
    anime = GetComponent<Animator>();
    rb2d = GetComponent<Rigidbody2D>();
}

Update the TakeDamage routine to the below. Previously, all it does was to start the Death routine. Now the Death routine will be triggered by the TankExploding animation. We will also be setting the rigidbody’s velocity to zero to stop it from moving anymore.

public void TakeDamage()
{
    currentHealth--;
    if (currentHealth <= 0)
    {
        rb2d.velocity = Vector2.zero;
        anime.SetTrigger("killed");
    }
}

Let’s try out with just one enemy tank to see if the explosion animation is ok.

Alrighty! Working nicely. Next post, we will create the Score Scene.

This article is Part 16 in a 25-Part Series.