Managing Health and Potions
Your hero's survival depends on proper health management. Let's learn how to use health potions effectively.
Basic Potion Usage
You can check if you have potions and drink them using these methods:
Manual Control
You can also bind potion usage to a keyboard key:
if (Input.GetKeyDown(KeyCode.F8))
{
Hero.DrinkHealthPotion();
}
// You can also continue doing other automatic actions
Hero.Idle();
Automatic Health Management
The most common use case is drinking potions when your health gets low. You can check your current health using the Health and MaxHealth properties.
if (Hero.Health < Hero.MaxHealth * 0.5f) // Below 50% health
{
if (Hero.HasHealthPotion())
{
Hero.DrinkHealthPotion();
Hero.RunAwayFromNearestEnemy(); // Good practice to retreat while drinking
}
}
Here's a more complete example that combines combat with health management:
if (Hero.Health > Hero.MaxHealth * 0.3f) // Above 30% health
{
Hero.RunAroundInAreaAndAttack();
}
else
{
Hero.DrinkHealthPotion(); // Start drinking potion
Hero.RunAwayFromNearestEnemy(); // Retreat while drinking
}
You can check how many potions you have using HealthPotionCount():
var potionCount = Hero.HealthPotionCount();
if (potionCount < 3) // Running low on potions
{
Hero.Say($"I only have {potionCount} potions left!");
}