Unity Game Development Blueprints
上QQ阅读APP看书,第一时间看更新

Adding in points, score, and wave numbers

One of the most important things to do in a game is reward the player and give them a sense of progression. For now, let's reward the player with a score we will display for them and also let the player know exactly which wave he is on. Perform the following steps:

  1. Create a new GUI Text object by going to the Hierarchy tab and selecting Create | GUI Text.

    Note

    If you are using the Unity 4.6 beta or higher versions, instead of creating the object in this way, you will need to select GameObject | Create Empty to create an empty object. Once you've done that with the object selected, then click on Component | Rendering | GUIText to add the component there.

  2. After this, switch to the Game tab, as you will be unable to see GUI Text in the Scene view. Note that the game should still not be started as yet, so don't hit the Play button. Have a look at the following screenshot:
  3. Change the GUI Text object's name to Score Counter. Under the GUI Text component, change the Text to Score: 0. After that, change the Position of Transform to (0, 1, 0) to put it on the left-hand side of the screen.

    GUI elements are placed in viewport space, which means that the space of the GUI is a value from (0,0) in the bottom-left corner to (1,1) in the top-right corner.

  4. Though this is technically fine, it would be nice to have some space so that the text isn't completely off the screen. To give us a bit of padding, let's set the Pixel Offset property to (10, -10) to move it 10 pixels toward the right-hand side and 10 pixels down. Note that Pixel Offset uses pixel space. Have a look at the following screenshot:
  5. Now that we have the text set up, let's set the font. Drag-and-drop the Font folder into your project. Then set Font to OSP-DIN and Font Size to 25.

    Note

    The font used in this project was created by the OSP Foundry. For more information about their stuff check out http://ospublish.constantvzw.org/foundry/.

  6. Next, duplicate the Score Counter object by right-clicking and selecting Duplicate. Set this duplicate's name to Waves Counter, and change its text to Wave: 0.
  7. Set the Waves Counter object's Position to (1, 1, 0). Then set Anchor to upper right and Pixel Offset to (-10, -10). Have a look at the following screenshot:
  8. Now that we have our text files created, let's now have them function correctly! First, let's go into the GameController class. Inside, we need to create some new variables as follows:
    // The values we'll be printing
    private int score = 0;
    private int waveNumber = 0;
    
    // The actual GUI text
    public GUIText scoreText;
    public GUIText waveText;
  9. Next, we will need to add a function to call whenever our score increases, as follows:
    public void IncreaseScore(int increase)
    {
      score += increase;
      scoreText.text = "Score: " + score;
    }

    The += operator will take the current value and add the right-hand side's value to it.

  10. Then, we'll need to call this function inside our EnemyBehaviour component. After the controller.KilledEnemy() line, add the following line:
    controller.IncreaseScore(10);
  11. Finally, whenever we increase the wave number we need to change its text as well. Back in the GameController class after the if(currentNumberOfEnemies <= 0) line, add the following lines:
    waveNumber++;
    waveText.text = "Wave: " + waveNumber;

    The ++ operator will take the current value of a number and increment it by 1.

  12. Save all the script files, go back to Inspector, and set the Score Text and Wave Text objects to the proper variables. After that, run the game. Have a look at the following screenshot:

And with that, you can see that everything is working together, killing enemies rewards points, and killing all the enemies in a wave triggers the next wave to start!