#Regen

1 messages · Page 1 of 1 (latest)

tulip fractal
#
using UnityEngine;
using UnityEngine.UI;

public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;
    public int currentHealth;
    public float flashDuration = 0.15f;
    public float fadeDuration = 0.4f;
    public float fadeSpeed = 1f;
    public float easingSpeed = 1f;
    public float maxHealth;
    public GameObject healthBarUI;
    public Slider slider;
    private float flashCounter;
    private Renderer rend;
    private Color originalColor;
    private float lastDamageTime;
    [SerializeField]
    public float regenerationCooldown = 3f;
    [SerializeField]
    public int regenerationRate = 1;

    // Use this for initialization
    void Start()
    {
        currentHealth = startingHealth;
        rend = GetComponent<Renderer>();
        originalColor = rend.material.color;
        maxHealth = startingHealth;
        slider.value = CalculateHealth();
    }

    // Update is called once per frame
    void Update()
    {
        if (currentHealth < maxHealth * 0.5f)
        {
            RunAway();
        }

        if (currentHealth < 0)
        {
            currentHealth = 0;
        }

        slider.value = Mathf.Lerp(slider.value, CalculateHealth(), Time.deltaTime * easingSpeed);

        healthBarUI.transform.LookAt(Camera.main.transform);
        healthBarUI.transform.rotation = Quaternion.LookRotation(healthBarUI.transform.position - Camera.main.transform.position);

        if (Time.time - lastDamageTime >= regenerationCooldown)
        {
            RegenerateHealth();
        }
    }

    float CalculateHealth()
    {
        return currentHealth / maxHealth;
    }

    void RunAway()
    {
        // code for running away
    }

    void RegenerateHealth()
    {
        if (currentHealth < maxHealth)
        {
            currentHealth += regenerationRate;
            slider.value = Mathf.Lerp(slider.value, CalculateHealth(), Time.deltaTime * easingSpeed);
        }
    }

    public void TakeDamage(int amount)
    {
        currentHealth -= amount;

        flashCounter = flashDuration;
        rend.material.color = Color.red;

        lastDamageTime = Time.time;

        if (currentHealth <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        // code for death
    }
}```
#

@solemn scroll here

solemn scroll
#

aaaaa hard to look at code that way, that's the first thing

tulip fractal
#

there

solemn scroll
#

thank you!

tulip fractal
#

np sorry I forgot lmao

solemn scroll
#

wait a bit pls

tulip fractal
#

sure

solemn scroll
# tulip fractal sure

really sorry for the wait. not the programming pro, but will try ->
I see it that way:

        if (Time.time - lastDamageTime >= regenerationCooldown)
        {
            RegenerateHealth();
        }

this is in Update. so when the condition is true it gets executed

past forge
#

Your regeneration rate is ~1, and if the game runs at 60 frames per second, you're calling this every frame, it will heal at 60 health per second.
As your setup is using ints, you would need to add another timer that makes it so you can only heal a certain amount over time.

solemn scroll
tulip fractal
past forge
#

you cannot do that because you are using ints.

tulip fractal
past forge
#

Add another timer, like I said, one that would apply the heal in increments

#
[Tooltip("The rate (in seconds) that Regeneration Amount is applied over time", Min(0.1f)]
[SerializeField] private float _regenerationRate = 1;
[Tooltip("The amount of health to regen per increment"]
[SerializeField] private float _regenerationAmount = 1;
// Internal timer to apply amount / rate hp
private float _regenerationTimer = 0;

...
void RegenerateHealth()
{
    if (currentHealth >= maxHealth)
        return;

    _regenerationTimer += Time.deltaTime;
    while (_regenerationTimer > 0) 
    {
        currentHealth = Mathf.Min(maxHealth, currentHealth + regenerationRate);
        _regenerationTimer -= _regenerationRate;
    }

    slider.value = Mathf.Lerp(slider.value, CalculateHealth(), Time.deltaTime * easingSpeed);
}```
Something vaguely like this
tulip fractal
#

with that?

past forge
#

You'll need to just read it and try to understand what I've written, and apply it yourself. If you're just replacing code without understanding, you are not learning

#

If you're confused about the while loop: it means that if multiple regen increments passed in a frame then the appropriate heal would still be applied.

tulip fractal
#

fair enough

#

thank you tho

tulip fractal
#

I understand the bottom but I can't tell if the top falls under a class or a void

#
using UnityEngine;
using UnityEngine.UI;

public class EnemyHealth : MonoBehaviour
{
    [Header("Health")]
    [SerializeField]
    public int startingHealth = 100;
    [SerializeField]
    public int currentHealth;
    [SerializeField]
    public float maxHealth;

    [Header("Flash & Fade")]
    [SerializeField]
    public float flashDuration = 0.15f;
    [SerializeField]
    public float fadeDuration = 0.4f;
    [SerializeField]
    public float fadeSpeed = 1f;
    [SerializeField]
    public float easingSpeed = 1f;

    [Header("Regeneration")]
    [SerializeField]
    [Tooltip("The cooldown period (in seconds) before the regeneration starts")]
    public float regenerationCooldown = 3f;
    [SerializeField]
    [Tooltip("The rate (in seconds) that the regeneration amount is applied over time")]
    public float regenerationRate = 1;
    [SerializeField]
    [Tooltip("The amount of health to regen per increment")]
    public float regenerationAmount = 1;

    [Header("UI")]
    [SerializeField]
    public GameObject healthBarUI;
    [SerializeField]
    public Slider slider;

    private float flashCounter;
    private Renderer rend;
    private Color originalColor;
    private float lastDamageTime;
    private float regenerationTimer;

    // Use this for initialization
    void Start()
    {
        currentHealth = startingHealth;
        rend = GetComponent<Renderer>();
        originalColor = rend.material.color;
        maxHealth = startingHealth;
        slider.value = CalculateHealth();
    }

    // Update is called once per frame
    void Update()
    {
        if (currentHealth < maxHealth * 0.5f)
        {
            RunAway();
        }

        if (currentHealth < 0)
        {
            currentHealth = 0;
        }

        slider.value = Mathf.Lerp(slider.value, CalculateHealth(), Time.deltaTime * easingSpeed);

        healthBarUI.transform.LookAt(Camera.main.transform);
        healthBarUI.transform.rotation = Quaternion.LookRotation(healthBarUI.transform.position - Camera.main.transform.position);

        if (Time.time - lastDamageTime >= regenerationCooldown)
        {
            RegenerateHealth();
        }
    }

    float CalculateHealth()
    {
        return currentHealth / maxHealth;
    }

    void RunAway()
    {
        // code for running away
    }

    void RegenerateHealth()
    {
        if (currentHealth >= maxHealth)
            return;

        _regenerationTimer += Time.deltaTime;
        while (_regenerationTimer > 0)
        {
            currentHealth = Mathf.Min(maxHealth, currentHealth + regenerationRate);
            _regenerationTimer -= _regenerationRate;
        }

        slider.value = Mathf.Lerp(slider.value, CalculateHealth(), Time.deltaTime * easingSpeed);
    }

    public void TakeDamage(int amount)
    {
        currentHealth -= amount;

        flashCounter = flashDuration;
        rend.material.color = Color.red;

        lastDamageTime = Time.time;

        if (currentHealth <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        // code for death
    }
}``` this is the best way I could add the code and it still has the same error
#

nvm i fixed it going off of the explanation