Forum begins after the advertisement:


Part 4 – How to stop healing if not enough mana to complete?

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity Part 4 – How to stop healing if not enough mana to complete?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13311
    Daikuna
    Participant

    Right now the code that is provided allows for you to start the healing animation/mechanic as long as you have more than 0 mana. This doesn’t check to see if you have enough mana to complete the action (ie. I have a 1 second timer for healing, the Mana Drain Speed is .33 and I have .32 mana. If I activate the healing it will use up all .32 mana but not heal because it never checked to see if I had enough mana to begin with)

    I’ve messed around and tried some different things but can’t get it figured out. Is this addressed in a later video and if not do you have any suggestions?

    #13312
    Daikuna
    Participant

    Think I figured it out but it’s not pretty. I’m sure there are much easier ways of doing this but this is what I came up with. `void Heal()
    {
    if(Input.GetButton(“Healing”) && (Mana * 10000) >= ((manaDrainSpeed * 100) * (100 – ((healTimer) * 100))) && Health < maxHealth && !pState.jumping && !pState.dashing){
    healTimer += Time.deltaTime;
    pState.healing = true;
    anim.SetBool(“Healing”, true);

    if (healTimer >= timeToHeal)
    {
    Health++;
    healTimer = 0;
    }

    Mana -= Time.deltaTime * manaDrainSpeed;

    }
    else
    {
    pState.healing = false;
    anim.SetBool(“Healing”, false);
    healTimer = 0;
    }
    }`

    I used large numbers to convert the fairly small floats to whole numbers and keep the accuracy pretty on point. I’m only about 5 days into learning C# so please let me know if there is an easier way to do this.

    #13316
    Terence
    Keymaster

    Hi Daikuna, this behaviour is intended.

    Even if you have enough mana to fulfill the mana drain speed, it will still not heal. The healing only occurs if you heal for a duration greater than timeToHeal. Otherwise, trying to heal will just drain your mana without healing you (like in Hollow Knight).

    If you want to prevent the user from healing if he does not have enough mana to heal any health, you will have to check your current mana against the timeToHeal * manaDrainSpeed value, like so:

        void Heal()
        {
            bool sufficientMana = Mana < manaDrainSpeed * timeToHeal;
            if (Input.GetButton("Healing") && Health < maxHealth && Mana > 0 && !pState.jumping && !pState.dashing && sufficientMana)
            {
                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;
            }
        }

    Hope this helps!

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

Go to Login Page →


Advertisement below: