PlayerHealth

PHOTO EMBED

Thu Apr 28 2022 14:08:16 GMT+0000 (Coordinated Universal Time)

Saved by @DanDHenshaw

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class PlayerHealth : MonoBehaviour
{
    public float health;
    public float maxHealth = 100f;
    public Image healthBar;

    private void Awake()
    {
        health = maxHealth;
    }

    public void TakeDamage(float damage)
    {
        health -= damage;

        healthBar.fillAmount = health / maxHealth;
        
        if (health <= 0)
        {
            Invoke(nameof(DestroyPlayer), 0f);
        }
    }

    private void DestroyPlayer()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;

        SceneManager.LoadScene("Lose");
    }
}
content_copyCOPY