niu-gdo / IGDO-Fall-2023-Project

IGDO's Fall 2023 game project.
7 stars 0 forks source link

Player Health Bar UI #41

Open ExplosiveEggshells opened 10 months ago

ExplosiveEggshells commented 10 months ago

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 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

Related Issues