Player lives and respawn

  • Create a new empty gameobject RespawnPoint. Position it where you want the Player to respawn
  • Create a DeadZone gameobject (below the platforms) with BoxCollider (increase scale in x, isTrigger = true) and Rigidbody (gravity= 0)
  • Create a DeadZone script with a reference for a Transform respawnPoint. In OnTriggerEnter, check out if other is Player.
    If so, try GetComponent<Player> .
    If it’s not null, Call player.PlayerDamage() and turn the gameobject off and on for respawning
  • In Player script, create an int lives variable, an Action<int> OnPlayerDied event and a method PlayerDamage() that will decrease the lives value by 1 and will call the event with the lives value
  • Subscribe the UIManager class to the OnPlayerDied event. In the method listener (for instance, UpdateLivesDisplay()) , Update the Canva’s playerLives text
  • Assign the references for playerLives text (Canvas) and RespawnPoint (DeadZone) in the inspector

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store