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 Text object by going to the Hierarchy tab and selecting Create | UI | Text.

    You'll notice that three objects get created at this point: Canvas, Text, and Event System. Right now, we only need to worry about Text; however, the three objects are all needed in order for Unity's new UI system to function properly. We will be discussing this in much more detail in a later chapter.

  2. Next, select the Text object again and notice that the top of it has a Rect Transform component instead of a Transform. This is a special transform that is used to position UI elements on the screen relative to other objects that are also part of the UI.
  3. In this case, we want the text to be located in the top-left, but currently you may not be able to see the text at all. You can double-click on the object to zoom out and see it more clearly.
    Adding in points, score, and wave numbers

    As you can see, the text is much larger than the player, and there seems to be a white box surrounding the object. This is a representation of what the UI will look like, with the white box being the screen. Rect Transform is currently set up to represent a position of (0,0) to be the center of the world and currently the text is -21 pixels away from the center in the x axis and -97 in the y. If we were to change it to (0,0), you'd notice that the text would now be centered.

    Adding in points, score, and wave numbers
  4. Rename the Text object to Score Counter and then change the Anchors Min Max and Pivot properties to (0, 1); then reset Pos X to 10 and Pos Y to -10 to move them slightly away from the edge of the screen. Lastly, change the Color to white to make it easier to see.
  5. Now that we have the text set up, let's set the font. Bring 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 Anchors Min Max and Pivot properties to (1, 1) and then reset Pos X to -10 and Pos Y to -10 to move them slightly away from the edge of the screen. Afterward, in the text component, change the Alignment to be right aligned horizontally.
    Adding in points, score, and wave numbers
  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 will first need to let the script know to use Unity's new UI system:
    using UnityEngine.UI;
  9. Afterward, we need to create some new variables as follows:
      [Header("User Interface")]
      // The values we'll be printing
      private int score = 0;
      private int waveNumber = 0;
      
      // The actual GUI text objects
      public Text scoreText;
      public Text waveText;
  10. 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 of the variable and add something additional to it.

  11. Then, we'll need to call this function inside our EnemyBehaviour component. After the controller.KilledEnemy() line, add the following line:
    controller.IncreaseScore(10);
  12. Finally, whenever we increase the wave number, we need to change its text as well. Back in the GameController class after the opening { of the if(currentNumberOfEnemies <= 0) block, add the following lines:
    waveNumber++;
    waveText.text = "Wave: " + waveNumber;
  13. Save all of the script files, go back to Inspector, and set the Score Text and Wave Text objects to the proper variables in the Game Controller object. After that, save your project and run the game. Have a look at the following screenshot:
    Adding in points, score, and wave numbers

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

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

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