Health and Armour Math Problems

So, im trying to put in a health and armour system, and im running into issues with the armour.
So lets say the damage sphere causes 10 hit points of damage. I have 10 points of armour and 100 points of health. I also have an armour strength variable that just changes to amount of damage taken - 5. So taking damage should result in my armour being 5. Then if I hit it again, I should have 0 Armour and 95 Health. I cant seem to figure that part out. Right now I have it so it just stops at 0, but i dont know how to get the value im looking for to take away from health… Heres the relevant code block

void OnTriggerEnter(Collider col)

    {

        if(player_hp < 100)

        {

            if (col.gameObject.tag == "Health") 

            {

                player_hp += 25;


                if (player_hp > 100) 

                {

                    player_hp = max_hp;

                }

                if (player_hp < 0) 

                {

                    player_hp = 0;

                }

            }

        }


        if(player_armour < 100)

        {

            if (col.gameObject.tag == "Armour") 

            {

                player_armour += 50;


                if (player_armour > 100) 

                {

                    player_armour = max_armour;

                }

            }

        }


        if(player_hp >= 0)

        {

            if (col.gameObject.tag == "Damage") 

            {

                if (player_armour == 0) 

                {

                    player_hp -= damageInput;

                }

                else 

                {

       

                    player_armour -= (damageInput - player_armourStrength);


                    if (player_armour < 0) 

                    {

                        player_armour = 0;

                    } 

               

                }

            }

        }

    }

Also I’m trying to make “player_armourStrength” just be equal to say, 90% of the damageInput instead of just having a solid number for it… how can I write this?

Thanks!

To have your amor as a percentage you will need to hold values for the current amount and for the max amount.
It might be an idea to have a method to fetch this information.

float getArmorStrength () {
    return (float)( (currentArmor / maxArmor) * 100)
}

As for taking damage…

int currentArmor = 100; //or whatever
int currentHp = 100;

void takeDamage(int damage){
    if(currentArmor > 0){ //check if we have armor first
        if(damage > currentArmor){
            int overflow = damage - currentArmor; //more damage than armor, we need to run the damage again after.
            currentArmor = 0;
            takeDamage(overflow);
        } else {
            currentArmor -= damage;
        }
    }else {
        currentHealth = Mathf.Max(currentHealth - damage, 0); //ensure no value below zero.
    }
}

It’s very quick and rough but I am more than happy to help further.

That would be 90% of the incoming damage:
player_armourStrength = 0.9f;
player_armour -= (damageInput * player_armourStrength);

But what you miss is the remaining damange. If armour is 20 and damage to it is 50, then the remaining 30 should be subtracted from health.

Also I would suggest to structure the code like shown below and use methods to split up logic.
Otherwise the only way to make damage or add health is with collission. But imagine you want a cheat code or other external influence, then you can simply call that method and need no collider.

void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Health")
        {
            AddHealth(25);
        }
        if (col.gameObject.tag == "Armour")
        {
            AddArmour(50);
        }
        if (col.gameObject.tag == "Damage")
        {
            AddDamage(damageInput);
        }
    }

Thanks!

Okay I’ll def restructure.

How would you go about approaching the overflow damage?

Thanks! I’m trying to implement this right now. I’ll ask if something comes up!