Falling Respawn & Damage- Game Dev Series 82
Objective: quick guide of damage system in platformer game.
Respawn at start position when falling out of game boundaries is an easy and balance option of gameplay.
In this article, we will create a simple and easy system to allow players respawn at the starting position when falling down.
Manage the dead zone & respawn position
First, we need to duplicate Player to create a new gameobject. Remove all the components except transform, which would be the respawn position afterward.
Then create a cube object and scale it at the bottom of whole game scene. Make the collider as trigger and add a rigidbody to it, this would be the dead zone detection.
Now create a new script called “DeadZone” and attach it to cube.
Inside this script, we will detect if Player has collided with. Before that, we need to add some codes to Player.
The definition of lives
In Player script, create a new integer variable as lives count. Then create a public method to update current lives.
Not only calculate the value in Player, but also in the UIManager. Create a public method in UIManager with a new text object in scene. Then our lives should update on the UI too.
And we can add more lines to Damage().
With these setting, we can scripting the DeadZone now.
Create a OnTriggerEnter() method and check if Player collided with. If it is Player, called Damage() to take 1 live from Player.
Create a gameobject variable as start position with SerializeField, and drag the gameobject into it in editor. Then replace the position of Player to Start Position.
However, if you play and test it, you would notice that it does not work. The falling speed of Player is too fast to make the CharacterController handle the position switching.
We need to disable the controller, and enable after Player is back to Start_position.
First, disable the controller before the position switching.
Then set the controller back to work in a coroutine.
Now Player should snapped back to where it started when falling out of game.