More elegant way to check for absorption on player

In my game I am creating the player can be shielded with a shield that absorbs x amount of damage. Basically when a player has a shield any damage going in to that player will have to pass a (is shielded) check. Then say the enemy deals 100 damage to the player and the shield absorbs 50 damage then I would obviously want the shield destroyed and the players health to be at 50.

The code works but I was wondering if there was a more elegant way to solve this problem.

Please let me know :slight_smile:

 public void TakeDamage(float Damage)
{
    if (shielded == true) {         
        float damageInflicted = Damage;
        if (damageInflicted > shieldAmount) {
            float damageRemaining = Damage - shieldAmount;
            shieldAmount -= Damage;
            currentHealth -= damageRemaining;
        } else {
            shieldAmount -= Damage;
        }
    } else {
        currentHealth -= Damage;
    }
}

I’d recomend changing shielded = false to shieldAmount = 0.
Not sure if you find it better, but it’s more compact:

     public void TakeDamage(float Damage)
     {
         currentHealth -= (Damage - shieldAmount);
         shieldAmount -= Damage;
         // Sets shieldAmount to 0 if negative
         shieldAmount = Mathf.Clamp(shieldAmount, 0, 10000);
     }