Today, I added health bars to player and enemies that follow them as they move. The bars are just a visual placeholder for now, and I’ll add health tracking functionality later.
Create a new canvas called “HealthBar” and add two images: one for “Background” and one for the “Fill”. The Background will be the outer frame, and the Fill will fit inside the Background, representing the health status.
Set the background’s width to 400 and height to 100, and change its color to black. For the Fill, set the width to 380 and the height to 80, and change its color to green.
Background
Fill
Next, set the width and height of the HealthBar canvas to 400 and 100. Change its Render Mode to “World Space” so that it can be positioned in the game world. Then move the HealthBar next to the player.
Next, move the HealthBar canvas to the Player GameObject. Once you run the game, the health bar will follow the player’s movement.
To give enemies health bars, I moved the HealthBar prefab to the Assets and attached it to each enemy GameObject.
No Responses