Upon completion of #40 , our character will have a useable and public health value that we can monitor with a UI element.
This issue will involve creating a screen-space UI element used to specifically display the player's health. A different issue will render health for other objects in the scene as well.
Detail
The UI
To create the screen-space UI, you will have to create a Canvas game object in the scene. We will then want to place down a Panel as a child of that Canvas, and finally a Text (Text Mesh Pro) as a child of that panel.
This will create a Text element within a panel (which we use as a backdrop) on the screen. Position the panel in the bottom right of the screen.
Fair warning, working with UI elements can be a little weird at first- so getting the panel anchored to the bottom right may be a little disorienting. Heavily recommending looking up a basic tutorial regarding making Unity UI. Start here.
The Script
In order to monitor our health, we will have to add a script to the text element to make it update it's rendered text to reflect the player's current health (CharacterHealthTextMonitor.cs
To do so, the script should on Start, use GameObject.FindGameObjectByTag("Player") to find the Player GameObject, then use GetComponent to access the attached PlayerInformation script.
Then, add a method which can respond to the OnHealthChanged action on PlayerInformation (which passes a float to the current health value). The method should grab TextMeshPro text component on the same GameObject and set it's text equal to the new health amount.
Of course, the UI text should also update itself on Start up as well (rather than waiting for a response from the first OnHealthChanged action).
Remarks
In your script, you will need to include using TMPro; to get access to Text Mesh Pro libraries.
Additionally, TextMeshPro Text UI elements have a strange class name: TextMeshProUGUI.
Depending on our codebase at the time, this could be difficult to test if there is nothing to actually do damage. In this circumstance, it would be sufficient to hook up a timer in the update loop to damage the player, or a button.
Related Issues
40 : Prerequisite - Must be completed before this issue can start.
Overview
Upon completion of #40 , our character will have a useable and public health value that we can monitor with a UI element.
This issue will involve creating a screen-space UI element used to specifically display the player's health. A different issue will render health for other objects in the scene as well.
Detail
The UI
To create the screen-space UI, you will have to create a Canvas game object in the scene. We will then want to place down a Panel as a child of that Canvas, and finally a Text (Text Mesh Pro) as a child of that panel.
This will create a Text element within a panel (which we use as a backdrop) on the screen. Position the panel in the bottom right of the screen.
Fair warning, working with UI elements can be a little weird at first- so getting the panel anchored to the bottom right may be a little disorienting. Heavily recommending looking up a basic tutorial regarding making Unity UI. Start here.
The Script
In order to monitor our health, we will have to add a script to the text element to make it update it's rendered text to reflect the player's current health (
CharacterHealthTextMonitor.cs
To do so, the script should on Start, use
GameObject.FindGameObjectByTag("Player")
to find the Player GameObject, then useGetComponent
to access the attached PlayerInformation script.Then, add a method which can respond to the
OnHealthChanged
action on PlayerInformation (which passes a float to the current health value). The method should grab TextMeshPro text component on the same GameObject and set it's text equal to the new health amount.Of course, the UI text should also update itself on Start up as well (rather than waiting for a response from the first OnHealthChanged action).
Remarks
using TMPro;
to get access to Text Mesh Pro libraries.TextMeshProUGUI
.Related Issues
40 : Prerequisite - Must be completed before this issue can start.