Auto Heal & Auto Recover Mana
💡
2
This is a implementation that can be used in all entities (hero, characters & monsters).
In CharacterBase.cs add the following methods
public int HealMissing()
{
return m_stats[EStat.Health] - m_currentStats[EStat.Health];
}
public int ManaMissing()
{
return m_stats[EStat.Mana] - m_currentStats[EStat.Mana];
}
private IEnumerator StartPeriodicalRecoverMana()
{
while (!this.dead && this.ManaMissing() > 0)
{
yield return new WaitForSeconds(recoverManaInterval); // Recover every X seconds
RecoverMana(1);
}
}
private void OnManaConsumed()
{
if (recoverManaCoroutine != null)
{
StopCoroutine(recoverManaCoroutine); // Stop any existing healing coroutine
}
recoverManaCoroutine = StartCoroutine(StartPeriodicalRecoverMana());
}
private void OnDamageApplied()
{
if (healCoroutine != null)
{
StopCoroutine(healCoroutine); // Stop any existing healing coroutine
}
healCoroutine = StartCoroutine(StartPeriodicalHealing());
}
private IEnumerator StartPeriodicalHealing()
{
while (!this.dead && this.HealMissing() > 0)
{
yield return new WaitForSeconds(healInterval); // Heal every 2 seconds
Heal(1);
}
}
private void StopCoroutines()
{
if (healCoroutine != null)
{
StopCoroutine(healCoroutine); // Stop healing if needed
}
if (recoverManaCoroutine != null)
{
StopCoroutine(recoverManaCoroutine); // Stop recover if needed
}
}
MORE IN THE COMMENTS` Complete file provided:
Still in the CharacterBase.cs add the following lines:
[Header("Recovery Intervals")]
[SerializeField] private float healInterval = 2f; // Time interval in seconds
[SerializeField] private float recoverManaInterval = 2f; // Time interval in seconds
An add this lines to the already existent methods in CharacterBase.cs:
public void ConsumeMana(int value)
{
...
OnManaConsumed(); //ADD THIS LINE IN THE END
}
public bool Damage(DamageOutputDescriptor damageOutput)
{
...
if (m_invincibleOnHit)
{
TryPlayInvincibleAnimation();
}
OnDamageApplied(); //ADD THIS LINE
...
}
protected virtual void Die()
{
GameManager.NotificationSystem.audioPlaybackRequested.Invoke(characterSheet.deathAudio);
StopCoroutines(); // ADD THIS LINE
...
}
Next update, i will include way to manage this “interval” throw character stats, so that you can add abilitites to manage it etc.

💬 Comments (0)
Be the first to comment! Join our Discord to share your thoughts.
Want to continue the conversation?