Player re-spawns after dying

2.5D Game Development

Objective: Player re-spawns and loses health after dying.

Add a cube gameobject in the scene, add box collider inside it and set its trigger to true and then add rigidbody and set the gravity to false. Now align the cube to below the playable zone and extend it as far as you want and set its mesh renderer to false.

Now create a new script and add it to the DeadZone gameobject. Inside the script, add a Transform variable which is going to store the respawn location. You can create a empty gameobject and place it to the location where you want to respawn and drag to to this variable.

Then create a trigger to detect player when player enters the collider. When inside, reduce the lives of player directly in the Game manager. Get a reference to the character controller and if the controller is not null then disable the character controller.

Then transform the player to the respawn point and start a coroutine.

This coroutine waits for 0.1 seconds before enabling the player and allowing it to move. We do this because during the fall, it is to fast for the trigger to complete all of its scripts before it exits so we disable the character controller to avoid the player from not de-spawning or even take damage two times.

Now inside the game manager, create a lives variable which stores the total life player has.

Then restart the scene after player has died and lives left is 0.

And this is the final result.

Unity Developer