Scripting the health bar

In this section, we will create a new script and attach it to our HUD's health bar. We will edit the script and use it to manage our Cucumber Man's health and the visible status of the HUD's health bar.

Let's start by reviewing our health bar:

  1. In the Hierarchy panel, select the HUD_Canvas | Health_Slider.

 

  1. In the Inspector panel, review the Slider (Script) component. As you can see in the following screenshot, there is a Value component with a slider at the bottom of the interface:
  1. Click the Game view tab so you can see the HUD without being in game-mode.
  2. In the Inspector panel, drag the Value slider and observe how the health bar functions.

You can see that the health bar already functions; we merely need to add a script to the slider so that it tracks the player's health and updates the HUD during game play. Here are those steps:

  1. In the Inspector panel, click the Add Component button
  2. Select New Script and name the script HealthManager
  3. In the Project panel, click Favorites | All Scripts
  4. Drag the HealthManager script to the Assets | Custom Scripts folder
  5. Double-click the HealthManager script to open it in an editor

 

  1. Edit the script so that it matches the following code, the first section of the code contains the namespace import statements and the HealthManager class declaration:
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HealthManager : MonoBehaviour {

The next section of code declares our class variables. We will use currentHealth to hold the value of the Cucumber Man's up-to-date health value. We will use healthBar as a reference to the slider:

public static int currentHealth;
public Slider healthBar;

Our HealthManager class has three methods; the first is the Awake() method. The first statement in this method gets a reference to the Slider component. The second statement sets the currentHealth to 100. This is our maximum health starting point for the Cucumber Man:

void Awake () {
healthBar = GetComponent<Slider> ();
currentHealth = 100;
}

Our second method is the ReduceHealth() method. This will be used by other scripts to call for a health reduction. As you can see, the first statement simply decrements the currentHealth value by one. The second statement updates the slider on the screen:

void ReduceHealth () {
currentHealth = currentHealth - 1;
healthBar.value = currentHealth;
}

The last method for this class is the Update() method. We have one statement in that method to update the slider for each frame of the game. This results in the slider accurately depicting the player's health:

void Update () {
healthBar.value = currentHealth;
}
}

In the next section, we will modify the appropriate script to call the ReduceHealth() method when the Cucumber Man is being bitten by a Cucumber Beetle. 

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

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