Choosing destinations – respawn to the most recently passed checkpoint

A checkpoint usually represents a certain distance through the game (or perhaps a track) in which an agent (user or NPC) has succeeded reaching. Reaching (or passing) checkpoints often results in bonus awards, such as extra time, points, ammo, and so on. Also, if a player has multiple lives, then often a player will be respawned only back as far as the most recently passed checkpoint, rather than right to the beginning of the level.

This recipe demonstrates a simple approach to the checkpoints, whereby once the player's character has passed a checkpoint, if they die they are moved back only to the most recently passed checkpoint.

Choosing destinations – respawn to the most recently passed checkpoint

Getting ready

This recipe builds upon the player-controlled 3D cube Unity project that you created at the beginning of this chapter. So, make a copy of this project, open it, and then follow the steps for this recipe.

How to do it...

To have the respawn position upon losing a life change depending on the checkpoints passed, follow these steps:

  1. Move the Cube-player GameObject to the (12, 0.5, 0) position.
  2. Select Cube-player in the Inspector panel and add a Character Controller component by clicking on Add Component | Physics | Character Controller (this is to enable the OnTriggerEnter collision messages to be received).
  3. Create a cube named Cube-checkpoint-1 at (5, 0, 0), scaled to (1, 1, 20).
  4. With Cube-checkpoint-1 selected, check the Is Trigger property of its Box Collider component in the Inspector panel.
  5. Create a CheckPoint tag, and assign this tag to Cube-checkpoint-1.
  6. Duplicate Cube-checkpoint-1 by naming the Cube-checkpoint-2 clone and positioning it at (-5, 0, 0).
  7. Create a sphere named Sphere-Death at (7, 0.5, 0). Assign the m_red material to this sphere to make it red.
  8. With Sphere-Death selected, check the Is Trigger property of its Sphere Collider component in the Inspector panel.
  9. Create a Death tag, and assign this tag to Sphere-Death.
  10. Duplicate Sphere-Death, and position this clone at (0, 0.5, 0).
  11. Duplicate Sphere-Death a second time, and position this second clone at (-10, 0.5, 0).
  12. Add an instance of the following C# script class called CheckPoints to the Cube-player GameObject:
    using UnityEngine;
    using System.Collections;
    
    public class CheckPoints : MonoBehaviour {
      private Vector3 respawnPosition;
    
      void Start (){
        respawnPosition = transform.position;
      }
    
      void OnTriggerEnter (Collider hit){
        if(hit.CompareTag("CheckPoint")){
          respawnPosition = transform.position;
        }
    
        if(hit.CompareTag("Death")){
          transform.position = respawnPosition;
        }
      }
    }
  13. Run the scene. If the cube runs into a red sphere before crossing a checkpoint, it will be respawned back to its starting position. Once the red cube has passed a checkpoint, if a red sphere is hit, then the cube will be moved back to the location of the most recent checkpoint that it passed through.

How it works...

The C# script class called CheckPoints has one variable called respawnPosition, which is a Vector3 that refers to the position the player's cube is to be moved to (respawned) if it collides with a Death tagged object. The default setting for this is the position of the player's cube when the scene begins—so in the Start()method, we set it to the player's position.

Each time an object tagged called CheckPoint is collided with, the value of respawnPosition is updated to the current position of the player's red cube at this point in time (that is, where it is when it touches the stretched cube tagged called CheckPoint). So that the next time the object tagged Death is hit, the cube will be respawned back to where it last touched the object tagged called CheckPoint.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.36.194