Implementing a life system | Unity
A quick guide about how to implement a life system with Unity
Objective: Implement a system to handle the lives of the player with Unity.
In the last post I covered how to create dynamic platforms with Unity. Now, it’s time to implement a way to handle the lives of the player when it falls in our platformer game.
Handling the fall
If you’ve seen my last posts you’ll remember that we’re using primitive assets to start creating a platformer game. For now, we have a player with some platforms and collectables in the next scene:
So, in order to handle when the player falls, let’s start by creating an empty gameobject that represents the point in the scene to respawn the player:
Then, let’s create a new gameobject that includes a box collider component with the Is Trigger property enabled to represent the zone where the player will lose a life:
Once the collider fulfills our purpose, let’s move the gameobject to the desired area:
Now, let’s create and attach a new script to the gameobject in order to handle the trigger collision with the player:
Then, let’s open the script and include the System namespace at the top:
Next, let’s create a new public static Action Delegate that will represent each fall of the player:
If you want to know more about Action Delegates you can visit the Microsoft docs:
Action Delegate (System)
Encapsulates a method that has no parameters and does not return a value. public delegate void Action(); public…
Now, let’s use the OnTriggerEnter method to be able to trigger the collision with other colliders. In this method, we’ll check if the collision was with the player by comparing the tag of the other collider. If the collider belongs to the player, we’ll call the Action Delegate (if it’s not null) to take the respective actions when the player is falling:
And now, in order to handle the life system, let’s open the player script and include the SceneManagement namespace as we’ll restart the level when the player runs out of lives:
Then, let’s create:
- A private variable store the total of lives of the player.
- A public property to return the value of the private variable.
- A private variable to store a reference of the respawn point.
Now, let’s create a new method to handle the damage on the player. When the method gets called it will:
- Subtract a life from the respective variable.
- Restore the level if the player runs out of lives.
- Disable the character controller component to smooth the respawn.
- Change the position of the player to be on the respawn point.
- Start a coroutine to enable the character controller after certain time.
The coroutine to enable the character controller is the next one:
Finally, let’s add the method to the Action Delegate from the dead zone on the Start method so that it gets called when the player falls. Also, as good practice, let’s remove it using the OnDestroy method to avoid trying to execute it if the player gets destroyed:
Once we saved the script, let’s drag the respawn point into the inspector:
Displaying the lives
Now, in order to display the remaining lives, let’s use a Text element inside our canvas like the one we use to display the coins:
Once we have the Text element, let’s open the UI Manager script and create a new variable to store a reference to it:
Then, let’s create a new method to update the lives by changing the text property and calling the lives property from the player class:
Finally, let’s add and remove the method from the Action Delegate like we did with the method to receive damage of the player class:
Don’t forget to drag the Text element to the respective variable through the inspector:
If we run the game in Unity, we’ll see that the player respawns and lose a life every time it falls:
And that’s it, we implemented a life system for our platformer game with Unity! :D. I’ll see you in the next post, where I’ll be showing how to create an elevator with Unity.