Displaying the Player Lives and Re-spawning when the player dies.
I am going to need a ScriptableObject Stat for the Player Lives. I will also need 2 Game Events. One for when the Lives Change so I can update the display when it changes and one for when I lose a life so the Player can be re-spawned when it dies. I also utilize costume Attributes.
Displaying the Lives
I Create a Text On My Canvas to Display the Number of Lives.
I then Create a Behavior to Display the Lives in A Text and add it to the Lives Text.
In the Player Lives Text Display I get the Text Component, I add a Game Event Listener so that when the Lives Change the Display will be Updated. When the Lives Stat is Changed I set the Text to Display “Lives: “ + The Number of Lives that I have.
I make sure to set the values in the Inspector.
Creating the Kill Zone
I want the Player to Lose a life and re-spawn at the start or at the last check point when they fall to far off the platforms into the vast void. I create a Trigger that covers the area the Player will be in if they fall.
I add a Kill Zone Component.
In the Kill Zone Behavior I check to see if the Player enter the trigger if it does I remove a life from the player lives.
Setting the Values in the Inspector.
It is now time to add a Game Manager to the level. The Game Manager will be responsible for re-spawning the Player when they die. As well as resetting the score and the player lives if this is the first level.
I then added the Code to re-spawn the player at the starting position. I created a private Game Object for the player and a bool to check if it is null. I also created a public Transform for the Spawn Point.
I moved all of the initialization code if this is the first level to it’s own method. I then Created a Method for Setting up the player. I find the player Game Object, next I check to see if the player is null if it is I return. If it is not I check to see if the spawn point is null If it is I set the Spawn Point to the player’s transform this saves the point that the player started at. If the the spawn point is not null then I call the On Player Died Method, This is also the Method that gets called when the Player lives Decreased Event is Raised.
In The On Player Died Method if there is a player then I set the players position and rotation to the spawn point position and rotation. If Has Player is true then there is a player and there is also a valid spawn point.
I set the Game Manger up using the Singleton Pattern and expose the spawn point transform. I make sure that if trying to set the spawn point outside of the Game Manager that it is not null as a valid transform is needed to set the players position and rotation. This will be needed in the future if I add a check point or save point in the level.
I add the Game Manger to the scene and set the values. I did not set the spawn point as I want to use the players transform as the spawn point.
Testing shows that I have made a mistake some place as the player did not move to the correct location. The lives did change.
The error is in the Kill Zone. I added -1 to the lives when I should have removed 1.
The next Issue is that I want to use the Players Starting transform as the spawn position, what is happening currently is when I set the spawn point to the players transform it is actually setting it to the location in memory (reference type) not the values of the transform (value type), pretty much what I am doing here is telling it to set the position and rotation to the position and rotation the Player is currently at. What I need to do instead is create an empty Game Object and set the transform values.
One last fix. I need to deactivate the Player before moving it and the reactivate it. This will stop any physics from happening on the player.
Now I can properly die and re-spawn in game.
I now need to take care of the what happens when there are no lives left. I will have the Game Manager load a scene that I can set in the Inspector.
Of course since I have no other scenes to load I will use this scene for now. Once I get the other scenes created I will have to come back and change this in the Inspector.
Now I can properly Die in Game.