My journey becoming a Unity game developer: 2.5D Infinite Runner-Player loses lives and Respawns at a particular location
Objective: Have the Player lose a life every time they fall below into a Dead Zone area. Also, respawn the Player back at its starting position and displaying how many lives are left onscreen.
To show the player’s lives onscreen, let’s duplicate the Coins text game object and use the settings from Coins text to give the same appearance for the new name of this object which will be Lives text. Inside the Text field under the Text component, change the text from “Coins: “ to “Lives: “ as this text will appear onscreen now. Move the Lives text object above the Coins text object so that both will be seen clearly.
Change the text fields of the Coins and Lives height so that they’re touching, but not crossing into each other boxes. We don’t want our text to interfere with each other if we make the Font Size bigger, and change the Vertical Overflow to Truncate.
Since we added an extra text field, the text in the lower-left corner has become a little crowded for the game’s visuals. Let’s move the text to the upper-left corner of the screen by selecting both Coins and Lives text objects, and changing the Anchor Presets pivot point and position.
Move the Coins text object below the Lives text object with the 2 fields boxes touching each other. We should now have a more uniformed look with the text onscreen.
Inside the Player script, create a new variable Type Int name lives set to 3 lives for the player to start with. Then, create a method that will have Public access with a Type Void name Damage(). Inside the Damage() method, we will decrement one life every time the player receives damage. Also, we will use the UI Manager script to get the Instance of this script and use the Update Lives Display() method to show how many lives the player has left.
Back in the Hierarchy, create a new 3D Cube object, and name it Dead Zone. This will serve as a location where if the player falls into it, the player will lose a life. Set its Transform positions to zero, then move the Dead Zone far enough down offscreen that we won’t see the player collide with the object. Also, Scale the X-axis wide enough across the screen so the player will have no choice but to fall into the Dead Zone. Last, remove the Mesh Renderer component as we will not need to see the Cube object onscreen. We only need the Box Collider.
Create another new script name Dead Zone, create a new variable Type Game Object name respawnPoint which will control the Respawn game object. Use the OnTriggerEnter() function to determine IF the object colliding Tag belongs to the Player. If it does, gain access to the Player’s component and check IF the Player is not NULL. If the Player isn’t NULL, use the Player’s Damage() method each time the player collides with the Dead Zone. Also when the Player dies, we want to set the Player’s new position to be at the Respawn object’s position.
In the Hierarchy, right-click on the Player game object and create a new Empty game object name Respawn Location. This object will automatically be a Child to the Player’s position, therefore we can remove it from under the Player object into its own spot in the Hierarchy. Now the Respawn Location will still be in the same position as the Player, but not under the Player’s control.
Attach the Dead Zone script to the Dead Zone game object itself. Drag the Respawn Location object into the Respawn Point slot in the Dead Zone script component. Turn on the Is Trigger setting for the Box Collider. Also, select the Canvas object and drag the Lives text object into the Lives Text slot under the UI Manager script component.
The Player can now fall into the Dead Zone and lose a life every time they fall into it. Plus, we will notice the Lives Text is updated onscreen every time the Player loses a life. However, when the player dies, the Player respawns back right away to the Respawn Location without any delay. This might look a little weird for the user, so let’s add a delay before the player can play again.
In the Dead Zone script, let’s gain access to the other object colliding Character Controller component which in this case we want the Player’s controller. Name the variable cc for Character Controller, and check IF the controller is not NULL. If it isn’t, we want to disable the controller by setting it to False so the Player properties are not available to them until we enable it again. After setting the Player’s position to the Respawn Point’s position after the Player dies, we will Start a Coroutine to wait for a certain amount of time before enabling the Character Controller by passing in the controller itself.
Create a new Coroutine using the IEnumerator name CC Enable Routine(), passing in the Type Character Controller name controller. We will wait for a half-second before enabling the controller by setting it to True.
Play the game now and watch when the Player dies. The Player is respawned at the player’s position when the game starts. Notice that the Player pauses for a half-second before gravity forces the Player to fall onto the platform. When the Player touches down on the platform, the user now has full control over the player in the game again.
One other thing we can do is Restart the game or level when the Player reaches zero lives. Inside the Damage() method, check IF the Player lives is less than one. If so, using Unity Engine’s Scene Management library, we can use Scene Manager to Load the Scene we currently are on which in this case is index 0.
Player loses all 3 lives, and level restarts all over again.
Okay, last thing we want to do is display the amount of lives and coins the Player will start off with. Inside the UI Manager, create 2 new variables name lives and coins. The lives variable will be set to 3 for how many lives the Player has, and the coins variable will be set to zero.
Then, inside the Start() function, use the Lives Text to display that Lives is set to 3 using the lives variable. Same with the Coin Text displaying that Coins is set to 0 using the coins variable.
We see the Lives displaying 3 at the start of the game, and Coins starting off at 0 as well.
Next time we will add to the Player’s ability using a new scene level with coins, moving platforms, a box to push on a pressure pad, jumping off walls, as well as an elevator to call so the player can move from one level to another.