Forum begins after the advertisement:


[Part 4] Mana is spent even without healing

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 4] Mana is spent even without healing

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #18315
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Reposting @stephanvieira3665‘s question from YouTube:

    Hello, if i only have a little a bit of mana, not enough to heal 1x Health, it still drains all the mana. Have you found any fixes for this? Thanks!
    void Heal()
    {
        if (Input.GetButton("Healing") && Health < maxHealth && Mana > 0 && Grounded() && !pState.dashing)
        {
            pState.healing = true;          
            anim.SetBool("Healing", true);
    
            //healing
            healTimer += Time.deltaTime;
            if (healTimer >= timeToHeal)
            {
                Health++;
                healTimer = 0;
            }          
            //drain mana
            Mana -= Time.deltaTime * manaDrainSpeed;
        }
        else
        {
            pState.healing = false;
            anim.SetBool("Healing", false);
            healTimer = 0;
        }
    }
    #18316
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    To address the issue where the Heal function drains all mana even when there isn’t enough to heal a full health point, you can add a check to ensure that the mana cost for healing does not exceed the available mana. Here’s a modified version of your Heal function:

    void Heal()
    {
        if (Input.GetButton("Healing") && Health  0 && Grounded() && !pState.dashing)
        {
            pState.healing = true;          
            anim.SetBool("Healing", true);
    
            // Check if enough mana is available for healing
            float manaCost = Time.deltaTime * manaDrainSpeed;
    
            if (Mana >= manaCost)
            {
                // healing
                healTimer += Time.deltaTime;
                if (healTimer >= timeToHeal)
                {
                    Health++;
                    healTimer = 0;
                }          
                // drain mana
                Mana -= manaCost;
            }
            else
            {
                // Not enough mana to heal, stop healing
                pState.healing = false;
                anim.SetBool("Healing", false);
                healTimer = 0;
            }
        }
        else
        {
            pState.healing = false;
            anim.SetBool("Healing", false);
            healTimer = 0;
        }
    }

    Changes Made:

    1. Mana Check: Before draining mana, the code checks if the current mana is greater than or equal to the cost of healing (manaCost).
    2. Stop Healing: If there’s not enough mana, the healing state is reset, and the animation is stopped.

    This way, the healing will only continue if there is sufficient mana to support it, preventing the drain of all the mana when it’s insufficient.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: