How to Indicate Health through Light Level of Torch

Hey all! I’m attempting to design a 2D platformer where the player’s health is indicated by the light level of their lantern. When the lantern is snuffed out = game over. I’m still fairly new to scripting and am unsure how I would approach this, if anyone has any advice or references that would be great, thank you! :slight_smile:

Hey tkeefauver,

You can do the following to get this concept to work.
Assuming you have a point light attached to your lantern model, you can create a script and attach it to the GameObject that has the health controller script (if you don’t have one yet, that’s ok).

The script to control the light intensity could have a function that looks something like this:

private float CalculateLightIntensity()
    {
        return (currentHealth / maxHealth) * lanternLight.intensity;
    }

This function can get called every time your character gets damaged.

A full script that controls the player health and the light intensity could look something like this:

public class PlayerHealth : MonoBehaviour
{
    public float maxHealth = 100f;
    private float currentHealth;
    public Light lanternLight;

    void Start()
    {
        currentHealth = maxHealth;
        lanternLight.intensity = CalculateLightIntensity();
    }

    void Update()
    {
        if (currentHealth <= 0)
        {
            GameOver();
        }
    }

    public void TakeDamage(float damage)
    {
        currentHealth -= damage;
        lanternLight.intensity = CalculateLightIntensity();
    }

    private float CalculateLightIntensity()
    {
        return (currentHealth / maxHealth) * lanternLight.intensity;
    }

    private void GameOver()
    {
        // Handle game over logic
    }
}