Updating the player's health
In this section, we will fully implement our player's health. We will start the Cucumber Man with three lives, each with a full health value of 100. We will update our scripts so that the Cucumber Man loses health when a cucumber beetle attacks him. Our approach will be to deduct one point from the Cucumber Man's health for every second that it is colliding with a Cucumber Beetle. We will also script the HUD's health bar and start with that task in the next section.
Before we get started, you should open your Unity game project. Alternatively, you can download theStarting-Chapter-11 Unity project available from the publisher's companion site.
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:
- In the
Hierarchy
panel, select theHUD_Canvas
|Health_Slider...