---
title: "Tank Health"
content_type: tutorial
url: "https://learn.unity.com/course/intermediate-3d-game-development/unit/tanks-tutorial/tutorial/tank-health?language=en"
language: en
container_type: course
container_title: "Intermediate 3D Game Development"
container_url: "https://learn.unity.com/course/intermediate-3d-game-development.md?language=en"
unit_title: "Tanks "
unit_url: "https://learn.unity.com/course/intermediate-3d-game-development/unit/tanks-tutorial?language=en"
standalone_markdown_url: "https://learn.unity.com/tutorial/tank-health.md?language=en"
previous_title: "Camera Control"
previous_url: "https://learn.unity.com/course/intermediate-3d-game-development/unit/tanks-tutorial/tutorial/camera-control?language=en"
previous_markdown_url: "https://learn.unity.com/course/intermediate-3d-game-development/unit/tanks-tutorial/tutorial/camera-control.md?language=en"
next_title: "Creating and Firing Shells"
next_url: "https://learn.unity.com/course/intermediate-3d-game-development/unit/tanks-tutorial/tutorial/creating-and-firing-shells?language=en"
next_markdown_url: "https://learn.unity.com/course/intermediate-3d-game-development/unit/tanks-tutorial/tutorial/creating-and-firing-shells.md?language=en"
difficulty: intermediate
duration_minutes: 45
unity_version:
  - "5.x"
supported_unity_versions:
  - name: "5.x"
    url: "https://learn.unity.com/tutorial/tank-health.md?language=en&version=5.x"
last_updated: "2026-08-04T14:24:03.000Z"
---

# Tank Health

Learn to implement and display the player tank’s health system in Unity’s Tanks project.

This phase of [Unity's Tanks project](https://learn.unity.com/project/tanks-tutorial) demonstrates how to create and display the health of the player's tank.

## Tank Health & UI

Phase 4 teaches you how to create and display the health of the tank.

[Video](http://www.youtube.com/watch?v=XRzGfRkZrNM)

## **TankHealth Script**

```csharp
using UnityEngine;
using UnityEngine.UI;

public class TankHealth : MonoBehaviour
{
    public float m_StartingHealth = 100f;               // The amount of health each tank starts with.
    public Slider m_Slider;                             // The slider to represent how much health the tank currently has.
    public Image m_FillImage;                           // The image component of the slider.
    public Color m_FullHealthColor = Color.green;       // The color the health bar will be when on full health.
    public Color m_ZeroHealthColor = Color.red;         // The color the health bar will be when on no health.
    public GameObject m_ExplosionPrefab;                // A prefab that will be instantiated in Awake, then used whenever the tank dies.


    private AudioSource m_ExplosionAudio;               // The audio source to play when the tank explodes.
    private ParticleSystem m_ExplosionParticles;        // The particle system the will play when the tank is destroyed.
    private float m_CurrentHealth;                      // How much health the tank currently has.
    private bool m_Dead;                                // Has the tank been reduced beyond zero health yet?


    private void Awake ()
    {
        // Instantiate the explosion prefab and get a reference to the particle system on it.
        m_ExplosionParticles = Instantiate (m_ExplosionPrefab).GetComponent<ParticleSystem> ();

        // Get a reference to the audio source on the instantiated prefab.
        m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource> ();

        // Disable the prefab so it can be activated when it's required.
        m_ExplosionParticles.gameObject.SetActive (false);
    }


    private void OnEnable()
    {
        // When the tank is enabled, reset the tank's health and whether or not it's dead.
        m_CurrentHealth = m_StartingHealth;
        m_Dead = false;

        // Update the health slider's value and color.
        SetHealthUI();
    }


    public void TakeDamage (float amount)
    {
        // Reduce current health by the amount of damage done.
        m_CurrentHealth -= amount;

        // Change the UI elements appropriately.
        SetHealthUI ();

        // If the current health is at or below zero and it has not yet been registered, call OnDeath.
        if (m_CurrentHealth <= 0f && !m_Dead)
        {
            OnDeath ();
        }
    }


    private void SetHealthUI ()
    {
        // Set the slider's value appropriately.
        m_Slider.value = m_CurrentHealth;

        // Interpolate the color of the bar between the choosen colours based on the current percentage of the starting health.
        m_FillImage.color = Color.Lerp (m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
    }


    private void OnDeath ()
    {
        // Set the flag so that this function is only called once.
        m_Dead = true;

        // Move the instantiated explosion prefab to the tank's position and turn it on.
        m_ExplosionParticles.transform.position = transform.position;
        m_ExplosionParticles.gameObject.SetActive (true);

        // Play the particle system of the tank exploding.
        m_ExplosionParticles.Play ();

        // Play the tank explosion sound effect.
        m_ExplosionAudio.Play();

        // Turn the tank off.
        gameObject.SetActive (false);
    }
}
```

## **UIDirectionControl Script**

```csharp
using UnityEngine;

public class UIDirectionControl : MonoBehaviour
{
    // This class is used to make sure world space UI
    // elements such as the health bar face the correct direction.

    public bool m_UseRelativeRotation = true;       // Use relative rotation should be used for this gameobject?


    private Quaternion m_RelativeRotation;          // The local rotatation at the start of the scene.


    private void Start ()
    {
        m_RelativeRotation = transform.parent.localRotation;
    }


    private void Update ()
    {
        if (m_UseRelativeRotation)
            transform.rotation = m_RelativeRotation;
    }
}
```
