Health Collectable

Marc Frappier
5 min readAug 26, 2023

--

Give the Player Extra Lives

As in all games, the Player has a finite number of lives. Each time the Player gets hit, they loose a life. Lets create a collectible that will give the Player back one life each time.

First, we must select an image that we want to represent the collectible. I chose a spaceship and added some text to it in a photo editor.

Extra Life Collectible

We must import this asset into our game. In the folder we already have called Powerups, I created a new folder called Life. I inserted the PNG file into this folder by dragging it there, you can also right-click the folder and select Import New Asset and select the file.

Importing Asset into Unity

We must now modify the options in order for Unity to recognize the file. Select the new asset file. In the Inspector, change the Texture Type to Sprite (2D and UI) and select Apply.

Changing Texture Type

Now we can drag our asset onto the Hierarchy so we can position it and add some Collider and Rigidbody.

Adding asset to Hierarchy

Select the asset in the Hierarchy. Now lets add a Rigidbody 2D and a 2D Box Collider. These are necessary in order to detect collisions and movement.

Adding Rigidbody 2D and Box Collider 2D

Make sure that the Gravity Scale in the Rigidbody 2D is set to 0 and that the Is Trigger is checked in the Box Collider 2D.

This collectible will behave just like our powerups, so lets also add the Powerup script.

Adding Powerup Script

Depending on the original size of your asset, you might want to make it bigger or smaller using the Scale of the Transform section. Increasing the x,y,z scale will make it larger.

Asset scale

Now lets drag the asset from the Hierarchy into the Prefab > Powerup folder.

Creating a collectible prefab

Open up the Powerup script. In the switch statement, we must add our new collectible.

What is going on here? Well, we need to assign the appropiate Powerup ID to our collectible so the switch statement will know what we are referring to.

Back in Unity, select the collectible prefab. In the Powerup script, there will be a Powerup ID with a default of 0, set it to 4.

In order for the Spawn Manager to find our new collectible, we must add it to the powerups array in our Spawn_Manager. Set the size to 5 and drag and drop the new asset into the new slot that appears.

Back in the SpawnManager script, which is where we randomly select an item from the array to spawn in the game, we need to modify the range of selection in the SpawnPowerupRoutine() so it includes our new asset. Make sure it is set (0, 5).

Back in our Powerup script where we modified the switch statement, we are calling the method AddLife(). Does this exist in our player script? No, so we need to create it.

What this method will do, is if the Player collects the Life, and his lives are less than 3, it will add a life to the Player. It should also update the visual effects (damage) on the Player’s ship as well as return a ship to his Lives sprite on the screen.

In the Player script, lets create the AddLife() method.

AddLife Method

When this method is called, the first thing it does is see if the Player’s lives are less than 3, meaning they have lost at least one life. If so, the _lives variable is incremented by 1, the sound effect for collecting the powerup is played and we call the UpdateLives method in the UIManager to show an extra life on the screen.

Each time the player is hit by an enemy laser or the enemy ship, they loose a life. The first time they get hit, the animation for the right engine appears, the second hit makes the left engine start to smoke.

Lets make it so when the Lives powerup is collected, depending on the level of damage, we reverse it by one.

Here we use the famous if else if statement. The first if statement will check if the player has only lost one life and will restore him to full health deactivating any damage they might have.

If the lives are not greater than 2, it checks to see if they are at least equals to 2, which means the player has lost two lives and has both engines smoking. Here, we will turn off the right left engine damage as if they had only been hit once.

Save the changes and run the game.

I hope you enjoyed this tutorial. See you in the next one.

--

--

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.