Setting game exit conditions – character death

The prototype still lacks a Heads-Up Display (HUD) completely. Heads-up display is a term used to refer to the collection of game information that is available for the player on the screen.

Health bars, lives indicators, speed references, and position references are all examples of common game HUD elements.

For this prototype, we plan to display two types of information on the screen: the number of collectibles gathered from the beginning of the match, and the number of available lives before the game ends. It's not by chance that we chose these two elements, as they are linked to the relevant variables to decide whether the player is winning or losing the game.

Before we can work on a game interface, however, we need to define the two exit conditions for our game: the "game over" and the "game won" conditions.

The plan is to have the game sending a Game Over message if the player loses three lives, and a Game Won message if they gather five collectibles. Let's begin with setting the condition for losing the game.

Getting ready

Let's say we want the player to lose a life whenever the character falls from a platform. This can be set up by adding a few lines to our PlatManager script.

How to do it…

  1. Open PlatManager in Monodevelop.
  2. In the Start() function, we already have a reference to the game character thanks to this line:
      thisChar = GameObject.Find("runner").GetComponent<Transform>();

    With this reference already set, we add a public variable to count the lives available to the player:

      public int lives;
  3. We also need a public float variable to store a reference value for our character's position. We will use it to check whether the character is falling:
    harpublic float yOffsetC
  4. We now can set the value of lives and yOffset to their default values. Add these lines to the Start() function of the script:
      lives = 3;
    yOffset = -37;
  5. Now scroll down to the FixedUpdate() function and add the following if() statement at the end of it. The if() statement check whether the character is falling down and, in case it does, it moves it back above the platform closest to it.
      if(thisChar.position.y < -yOffset){
        
        lives -= 1;
    
        Vector3 v = new Vector3 (0,0,0);
        if(nextPlat){
          v = nextPlat.position;
        }
        else{
          v = actualPlat.position;
        }
    
        v.y += 40;
        thisChar.position = v;
      }

How it works…

The if() statement in FixedUpdate() checks at every frame the character's y position. If this value goes beyond the threshold provided with yOffset (-37 in our case), it means that the character is falling, so the script takes 1 away from the number of available lives and moves the character back to a safe position.

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

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