Healing Spell (Heal when using mana)

,

Hello everybody!

I am trying to figure out how to do a “healing spell” for my game.

What I want to do is: for each x amount of mana spent, heal 1 HP.

I am using a heart system for the health and draining mana using:

CurrentMana -= amountToSpend * Time.deltaTime;

I am looking on google and YouTube how to do it, but I can only find how to do healing VFX :confused:

Any ideas on how to do this?

Thanks! :smiley:

Hi,

Not exactly sure what you mean? a spell that consumes all mana and heals for that or?

If you want some heal over time, here is a topic where Brian gives some answers on it;

This is how i did it myself before i found that thread;

Maybe if you use the search function, with RPG tag and heal keyword you could find some more posts,
maybe something you want in there!

Thank you for your answer!

I also searched here on the forum but couldn’t find anything so that’s why I’m posting my question.

Sorry if I didn’t explain myself better.

Basically what I want is:

  1. Spend mana over time (delta time) when pressing input (this part is already working).

  2. Every 30 mana point drained, add 1 health point.

I’m not sure how to implement that.

Thanks!

This is how I would do it to avoid balance issues, like the player spending mana when it cannot reach the required amount to heal, and also to prevent the player from spending mana with no effect because the player didn’t hold down the button the right amount of time.

  • Cache a float, I’ll call it timePressed.
  • If mana is not enough to trigger the healing spell, return.
  • If button is held down
    • Add delta time to timePressed
    • If timePressed >= the amount of time needed to trigger the spell.
      • Trigger the spell, spending mana and healing.
      • Set the timePressed value to 0.

Ah, so not per se focussed for use in the RPG course?
How did you have the mechanics in mind?

Like;
While you hold a button it will keep consuming? or a toggle?
Or a onetime cast that runs for X seconds
And how long will it take for one hp to be restored etc?

This for example, would if you press 1, take 3 seconds to heal 1HP for a cost of 30,
or if you hold 1, it would heal 1HP every 3 sec, as long as you have mana.
you could replace the keypress with a bool or something, to make it a toggle.

    [SerializeField] float maxHealth = 100f;
    [SerializeField] float maxMana = 100f;
    [SerializeField] float manaCost = 30f;
    [SerializeField] float healAmount = 1f;
    [SerializeField] float castTime = 3f;

    bool isHealing = false;
    float currentHealth;
    float currentMana;

    void Start()
    {
        currentHealth = maxHealth;
        currentMana = maxMana;
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.Alpha1) && currentMana > manaCost && !isHealing)
        {
            StartCoroutine(ProcessHealSequence());
        }
    }

    IEnumerator ProcessHealSequence()
    {
        isHealing = true;
        currentMana-=manaCost;
        yield return new WaitForSeconds(castTime);
        currentHealth += healAmount;
        isHealing = false;
    }
1 Like

Just to let all involved know, I’m keeping an eye on the topic, to see how things develop, but I think you guys are on the right track so far.

1 Like

So I tried your solution but unfortunately it’s not working as I intended :frowning:
Thank you very much for your help tho :smiley:

I have tried a few other thigs but I can’t make it work either.

This is my code so far:

 private void Update()
        {           
            if (InputReader.Instance.HealInput) //While holding  heal input
            {                
                UseQiToHeal();
            }                
        }

 private void DrainMana()
        {
            CurrentMana -= Time.deltaTime * DrainSpeed;
        }

private bool HasDrainedEnoughMana()
        {
            if (CurrentMana == CurrentMana - ManaAmountForHealth)
            {
                return true;
            }

            return false;
        }

private void UseManaToHeal()
        {
            DrainMana();            

            if (HasDrainedEnoughMana())
            {
                AddHealth(1);
            }           

        }

Basically, what I want to happen is:

  1. Drain mana over time (Time.deltaTime * DrainSpeed) when holding input.

  2. If has drained x amount of mana (ManaAmountForHealth), then add 1 Health.

I managed to do it! :partying_face:

const float ManaAmountForVitality = 33f;

float startingMana;       

private void Update()
{

            if (InputReader.Instance.HealInput )
            {
                UseManaToHeal();
            }
            else if (!InputReader.Instance.HealInput)
            {
                startingMana = CurrentMana;
            }

}

public void DrainMana()
{
            CurrentMana -= Time.deltaTime * DrainSpeed;
}

private bool HasDrainedEnoughMana()
{
            if (CurrentMana <= startinMana - ManaAmountForVitality)
            {
                return true;
            }

            return false;
}


private void UseManaToHeal()
{
            DrainMana();

            if (HasDrainedEnoughMana())
            {
                startingMana = CurrentMana;
                Vitality.AddVitality(1);
            }
}


Unless ManaAmountForHealth is Zero, this statement will never be true. Try adding a ManaSpent variable. So DrainMana will say

CurrentMana-=Time.deltaTime * DrainSpeed;
ManaSpent+=Time.deltaTime* DrainSpeed;

Then HasDrainedEnoughMana will read:

return ManaSpent>=ManaAmountForHealth; //Yep, that's the whole function

And finally, in the if(HasDrainedEnoughMana()) block:

{
    AddHealth(1);
    ManaSpent = 0;
}

LOL, now that I typed all that, I see that you found a working solution as well. Well done!

1 Like

I actually like this solution better! Thank you :smiley:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms