Day 80 — Implementing Lives and Respawning

Connor Fullarton
3 min readJun 3, 2021

Hey and welcome!

Before I get onto the next batch of content I’m making an article here on implementing lives into our game as well as the ability to respawn at the starting position if the player falls out of the map, both of these things we’re no stranger to since they were done during the 2D Space Shooter.

First things first go ahead and copy the Player object and remove all the components from the object, this is what we’re going to use to designate the start position. Next create the text for the lives, similar to what you did for the coins and adjust it to your liking.

With that done add in this code to the UIManager script:

public Text livesText;public void UpdateLives(int lives)
{
livesText.text = "Lives: " + lives;
}

Now add in the lives text that you created as the text object that this is looking for and you’ll be all set for the text/UI part of this.

In order to simulate death for our player, let’s create a kill plane that stretches out the whole length of our level just below all of our platforms.

Turn off the mesh renderer so that the player can’t see it, set it to a trigger collision and then add a Rigidbody component with the gravity off to finish this step.

With that done we need to create a script for our kill plane and then add in all of the following code:

using UnityEngine.SceneManagement;public GameObject _startPosition;
private int _lives = 3;
private UIManager _uiManager;
void Start()
{
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_lives--;
_uiManager.UpdateLives(_lives);
CharacterController cc = other.GetComponent<CharacterController>();
if (cc != null)
{
cc.enabled = false;
StartCoroutine(CCEnableRoutine(cc));
}
other.transform.position = _startPosition.transform.position; if (_lives < 1)
{
SceneManager.LoadScene(0);
}
}
}
IEnumerator CCEnableRoutine(CharacterController controller)
{
yield return new WaitForSeconds(0.5f);
controller.enabled = true;
}

In order to not confuse things too much I’ve kept all the code in the one script here but ideally you’ll want the player script to handle the lives as well as letting the UIManager know that they need updating. Similar to the CharacterController you can do Player player = other.GetComponent<Player>() in order to get a reference to the player script.

In this script though, we handle what happens when the player collides with this kill plane object which is to subtract 1 from the lives and respawn the player to the start position that we set before (make sure to drag and drop that in the inspector!).

Something a bit new though is that we disable the character controller and then re-enable it half a second later once the player position has been set back to the start position. The reason why we disable the controller in the first place is because the velocity can be too great which leads to the player not respawning at the start position even when they collide with the kill plane.

Last thing, make sure that you add your scene in the build settings otherwise LoadScene(0) won’t work when the lives is less than 1.

With all that done you should have something like the above working for you!

--

--

Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.