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

Continuing with the Bonus Crates. We will touch on the Invincibility bonus crate which is symbolized by the helmet symbol. Its effects is to grant the PlayerTank a prolonged period of invincibility.

Start by dragging and dropping the Sprite you have for invincibility bonus crate into the hierarchy which Unity will help to create the Game Object for you. Call the Game Object Helmet.Add a Box Collider 2D and assure its boundary fills the entire crate’s image, then make it a trigger. Also, assign Powerups layer to the Game Object. You should also set its Order In Layer to 2 to ensure it gets rendered on top of all objects.

Adjust the sprite's Pixels Per Unit so that the Game Object fills the same amount of area as a tank.

Creating the invincibility effect

There is not much we need to do about the invincibility effect as it is just an extension of the electric aura the tank has when it first gets spawned. So all we need to do is to create a state in the Animator and add the TankCreated animation to it.

Drag and drop the PlayerTank prefab to the hierarchy and select it. Open up its Animator Window and right-click->Create State->Empty. Name the State as Invincibility.

Create a new Parameter called Invincible.

  • Right-click on the Entry and select Make Transition, drag the arrow to Invincibility. After the boxes are joined together by the arrow, click on the arrow and add condition Invincible to it.
  • Do the same for TankCreating box to Invincibility Box, but this time also uncheck Has Exit Time.
  • Do the same for Normal box to Invincibility Box, also uncheck Has Exit Time.
  • Lastly, right-click on the Invincibilty and select Make Transition, drag the arrow to Normal.

After the boxes are joined together by the arrow, click on the arrow and draw down the Settings in the Inspector and set Exit Time to 10. This will mean the animation will run for 10 seconds before moving back to Normal.

Now click on the Invincibility Box again and set the Motion in the Inspector to TankCreating.

Coding the invincibility

Now we can creating the triggering of the invincibility. Create a script called Helmet in the Helmet Game Object. Set the class to inherit from PowerUps. The full code for Helmet as below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Helmet : PowerUps
{
    // Use this for initialization
    protected override void Start()
    {
        base.Start();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        collision.gameObject.GetComponent<Animator>().SetTrigger("Invincible");
        Destroy(this.gameObject);
    }
}
Everything is the same as the OneUp script with exception from the part from OnTriggerEnter2D. We will simply get a reference to the Animator of the collided Game Object(which can only be the PlayerTank because of the Layer Collision Matrix changes we made in Physics 2D Settings) and set trigger Invincible. Once that is done, it will destroy the game object.

That’s all! We can test it already. Create a prefab of Helmet by dragging it to the Project Window. Go to the Canvas Game Object and drag and drop the Helmet prefab to the Bonus Crates under Game Play Manager (Script), replacing the 1up prefab.

Then press play!

Testing successfully! Next stop, will be on the Grenade Bonus Crates.

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