lundi 4 février 2019

Health Slider If Statement Line Renderer Enabled Unity

My Enemy script activates a raycast and line renderer when within range of the player. However using the Health script below my health slider continues to decrease in value after my enemy is destroyed or I move the player out of range.

New to unity, hope this is not too simple. I guess its cause there is nothing after CurrentHealth -= Time.deltaTime * 10; to stop the health decreasing but how would I fix this?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Health : MonoBehaviour
{

public Text HealthText;
public Slider HealthSlider;

public float MaxHealth = 100;
static public float CurrentHealth = 100;

public LineRenderer lineRenderer;



private void Update()
{
    HealthUI();


    if (CurrentHealth > 0)
    {
        if (lineRenderer.enabled) 
        {
            CurrentHealth -= Time.deltaTime * 10;   
        }
    }
}   

public void HealthUI()
{
    HealthSlider.value = CurrentHealth / MaxHealth;
    HealthText.text = "HEALTH " + ((int)(CurrentHealth / MaxHealth * 100)).ToString() + "%";
}  

}

Thanks!

Aucun commentaire:

Enregistrer un commentaire