Respawn Points in Unity 3D

It would be nice if we could move the player to a respawn point, after a loss of life. We'll do that by setting up an empty game object. Then we can just set the player's coordinates to the transform of the empty game object. Let's see how it's done.

So, right-click on a blank spot in the Hierarchy. From the menu, select Create Empty:

Unity menu showing the Create Empty item selected

Give your empty object the name RespawnPoint. In the Inspector, set the X, Y and Z values to where you want your player to respawn. We had these values for the Position:

X: 16
Y: 0
Z: 17

This will put the empty object where the player is now. His starting position, in othe words.

We need to tag this empty game object. Fortunately, there is already a tag set up called Respawn. So, make sure your empty game object is selected in the Hierarchy. In the Inspector, click the Tag dropdown at the top. Select Respawn:

Setting a tag for the respawn point

Go back to your PlayerHealth script. Add these two new variables at the top, with all the others:

private Transform spawnPoint;
private Transform playerPos;

These two variables are both Transform ones. The first we've called spawnPoint and the second we've called playerPos.
In the Start method, add these two lines:

playerPos = GameObject.FindGameObjectWithTag("Player").transform;
spawnPoint = GameObject.FindGameObjectWithTag("Respawn").transform;

Here, we're just finding two objects with particular tag names, Player and Respawn. We want to access the transform for both. These then end up in our two Transform variables playerPos and spawnPoint.

Now we only need to set the player's position to the spawn point. Add this if statement to your PlayerTakeDamage method, just below the playerHealth line:

if (playerHealth <= 0) {

playerHealth = maxHealth;
playerPos.position = new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z);

}

The code should look like this in your editor (new lines are highlighted):

Unity C# code for a respawn point

Let's go through PlayerTakeDamage method to see what's happening.

The first line is the same as before - take points off when the player takes damage:

playerHealth = playerHealth - damage;

Next, we have an if statement:

if (playerHealth <= 0) {
}

This checks if the value in playerHealth is less than or equal to zero. If it is, we first have this:

playerHealth = maxHealth;

This resets the playerHealth back to the maximum health, which is set to 100 at the top of the code.

The line that resets the player is this rather long one:

playerPos.position = new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z);

We're resetting the player's position. After the equal sign, we have this:

new Vector3(spawnPoint.position.x, spawnPoint.position.y, spawnPoint.position.z);

The player's position needs a new Vector3, if you're going to reset it. In between the round brackets of Vector3, you need an x, a y, and a z value. These all come from the empty game object we set up, which is now in the spawnPoint variable. All three values use position again:

spawnPoint.position.x
spawnPoint.position.y
spawnPoint.position.z

We'll need to add to the PlayerHealth script later, when we do number of lives and game over. For now, save your code and go back to Unity. Select the First person controller object in the Hierarchy. In the Inspector, change the Max Value to something lower, just for testing purposes. Otherwise, with a default value of 100, you'll be waiting around until soldier hits you 10 times before you die. At a value of 20, you'll die in 2 shots.

Now test it out. Play your game. Walk forward a bit until the player starts shooting. When he kills you, you should find you'll be reset to the respawn point and the player will stop shooting.

Now let's do some health pickups. Change the Max Health back to 100 before moving on, though.

<--Back to the Unity 3D Course Contents Page