Home Tower Defender - Part 5
Post
Cancel

Tower Defender - Part 5

Enemy health bar and damage

The purpose of the blog is to document my journey in building a simple Tower Defense game. In this fifth part I show how to add a health bar that update it when taking damage.

The following Unity / C# features will be covered:

Setup a UI Slider in the Unity Interface

In order to see the damage, a health bar needs to be added to the Enemy. A good way to do this is via UI -> Slider. I have changed the Fill / Background color and un-ticked the Active mark, so the slider won’t be visible at the start of the game but only becomes visible after the first hit.

UI Slider HealthBar

Explain logic for the EnemyHealthController script

This script controls the UI Slider that is placed above the Enemy prefab and changes it’s value based on the damage taken.

Variables defined at the start:

  • Total enemy health with a start value of 10. Can be adjusted in the editor.
  • A UI field to assign the UI slider too in the editor.

Custom Function - InitializeHealthBar:

  • Sets the maximum value of the health slider to the total health of the enemy.
  • Sets the slider value to the total health as well. This is used for the start of the game.

The Update method:

  • Calls the custom defined UpdateHealthBarRotation method.

Custom Function - UpdateHealthBarRotation:

  • This function assigns the healthBar slider rotation to the same direction as the Camera rotation. This is done so the health Slider keeps in the viewer position, even if the enemy changes direction.

Custom Function - TakeDamage:

  • Reduces the total health value after damage is taken.
  • Sets the Slider on active (visible) after the first hit.
  • If the total health reaches zero or below, the enemy game object will be destroyed.
  • Updates the Slider value based on updated total health value.

The complete C# code for the the tower placement script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using UnityEngine;
using UnityEngine.UI;

public class EnemyHealthController : MonoBehaviour
{
    [SerializeField] private float totalHealth = 10f;
    [SerializeField] private Slider healthBar;

    private void Start()
    {
        InitializeHealthBar();
    }

    private void InitializeHealthBar()
    {
        healthBar.maxValue = totalHealth;
        healthBar.value = totalHealth;
    }

    private void Update()
    {
        UpdateHealthBarRotation();
    }

    private void UpdateHealthBarRotation()
    {
        healthBar.transform.rotation = Camera.main.transform.rotation;
    }

    public void TakeDamage(float damageAmount)
    {
        totalHealth -= damageAmount;
        healthBar.gameObject.SetActive(true);

        if (totalHealth <= 0)
        {
            totalHealth = 0;
            Destroy(gameObject);
        }

        healthBar.value = totalHealth;
    }
}

Game preview

Tower rotate to enemy

This post is licensed under CC BY 4.0 by the author.