2.5 Platform: Implementing Lives
Implementing life for the player!
For today I implemented lives for the player. This comes with the handling of player lives and what to do when the player dies. The idea is when the player falls off the map, lives is decremented and the player will respawn back to the starting position. And if the player has zero lives, then restart the scene.
I created a variable called:
[SerializeField] private int _lives = 3;
I serialize field to expose it in the inspector and gave it a value of 3.
Since I want to display lives on the screen, I created a method to return the value of lives:
public int GetLives() => _lives;
I went ahead to create a simple UI text to display the lives over the coins.
I then dragged the text to the UIManager component in the UI Manager game object to pass it as a reference.
In my UIManager script, I added:
[SerializeField] private Text _livesText = null;
To update the UI text, I added:
private void DisplayText()
{
_coinText.text = “Coins: “ + _player.GetCoinAmount().ToString();
_livesText.text = “Lives: “ + _player.GetLivesAmount().ToString();
}
DisplayText method is called in the Update method.
private void Update()
{
DisplayText();
}
With lives all set up and displaying properly, I created a kill zone game object with a trigger box and placed it underneath the lowest platform. This area will trigger the respawn point and decrement the lives when the player falls to it.
I created a script for the trigger and called it KillZone.cs. This will hold a member for the respawn point and check if the player has collided with the trigger collider. I also attached it to the kill zone game object.
I added:
[SerializeField] private Transform respawnPoint;
This will hold a reference for the respawn position.
We also want to check if the player has collided with the trigger so in this case, I use the on trigger enter method Unity provides for collision checking.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(“Player”))
{
Player player = other.GetComponent<Player>();
if(player != null)
{
player.Damage();
}
}
}
If the other collider hitting this trigger is the Player, then we want to apply damage and respawn the player.
In the player script, I added:
public void Damage()
{
_lives--;if (_lives < 1)
Restart();
}
I want the lives to decrement each time the player hits the Killzone. And if lives is less than 1, restart the scene.
private void Restart()
{
SceneManager.LoadScene(0);
}
A simple method to restart the scene is all I need for now.
For the player to respawn at the respawn position, I added:
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(“Player”))
{
Player player = other.GetComponent<Player>();
if(player != null)
{
player.Damage();
} player.transform.position = respawnPoint.position;
}
}
Because I am using the character controller unity provides us for player movement, I need to disable the component from the player game object register the OntriggerEnter method completely upon trigger detection. Then enable it after respawning. This will ensure the respawning of the player.
So I created a coroutine:
IEnumerator ControllerEnableRoutine(CharacterController controller)
{
yield return new WaitForSeconds(0.5f); if (controller != null)
controller.enabled = true;
}
After the player hits the trigger:
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(“Player”))
{
Player player = other.GetComponent<Player>();
if(player != null)
{
player.Damage();
}CharacterController controller = other.GetComponent<CharacterController>();
if (controller != null)
controller.enabled = false; player.transform.position = respawnPoint.position; StartCoroutine(ControllerEnableRoutine(controller));
}
}
I want to access the character controller component, disable the controller so that the player can respawn, then re-enable it after respawning for half of a second.
With all of this in place, my player now has logic for a lives system.