How to carry over remaining damage hit points from armor to health?
Hello. Just wondering how would I go about applying remaining damage hit points from armor to health? Example: Current armor is (5)points and damage applied is (10) points, while current health is (100) points, than current armor should be set to (0) points and current health should be set to (95) points. But I can not get this to happen. Instead armor would take all remaining damage points. this wouldn't be good in the case of; Armor points is (1), Health points (100) and Damage is (200). Player would take damage and would still be alive with (100) Health points. My example code:
public int maxHealth = 100;
public int currentHealth;
private int minHealth = 0;
public int maxArmor = 100;
public int currentArmor;
private int minArmor = 0;
void Awake ()
{
currentHealth = maxHealth;
currentArmor = minArmor;
}
// Update is called once per frame
void Update ()
{
if (currentArmor < minArmor)
{
currentArmor = minArmor;
}
if (Input.GetKeyDown(KeyCode.A))
{
if (currentArmor > 0)
{
currentArmor -= 10;
}
else
{
currentHealth -= 10;
}
}
}
Answer by Measurity · Dec 26, 2015 at 11:11 PM
I recommend creating a function that will calculate and apply the damage for you in the component that handles this:
void Update ()
{
if (Input.GetKeyDown(KeyCode.A))
{
Hit(13);
}
}
public void Hit(int damage)
{
int armorDamage = Math.Min(Armor, damage);
int healthDamage = Math.Min(Health, damage - armorDamage);
Armor -= armorDamage;
Health -= healthDamage;
}
Now when damage is applied. It first substracts from the first layer (Armor) then from the second (Health). This way you can easily add multiple layers of protection (like shields).
This was just an example but I do have a function that will calculate and apply the damage. How ever, what is written in your function above is what I am looking for. $$anonymous$$uch thanks to you.
Your answer
Follow this Question
Related Questions
Health Script 2 Answers
Destroy 2D obj issue 3 Answers
How to make a if statement refernce mulitple int numbers 0 Answers
Urgent help with Health System in a 2D game 1 Answer
How to make a particle effect do damage? 4 Answers