Trying to Implement Elemental Damage But Introduced Bug

Believe it or not, I believe that the text is working exactly as intended.

Your Heretic’s Sword is too strong, often killing the enemy on the first hit. The way TakeDamage is originally written, we don’t fire the takeDamage events if the enemy dies on the hit. When I lowered the weapon’s main damage from 20 to 5, I got the damage text right away.

Now that’s something I actually don’t like about the way Sam structured the damage text firing. So here’s my rewritten TakeDamage method to deal with that.

        public void TakeDamage(GameObject instigator, float damage, string damageInfo = "")
        {
            if (isDead) return;
            float oldHealth = health.value;
            health.value = Mathf.Max(health.value - damage, 0);
            {
                if (health.value <= 0)
                {
                    float overKill = damage - oldHealth;
                    string overKillText = overKill > 0 ? $"({overKill} overkill)" : "";
                    takeDamageText?.Invoke($"{damage} {damageInfo} {overKillText}");
                    Die();
                    AwardExperience(instigator);
                }
                else
                {
                    takeDamage?.Invoke(damage);
                    takeDamageText?.Invoke($"{damage} {damageInfo}");
                }
                
            }
            isHurt = true;
        }

This will do several things:
If the character is already dead, then the method will exit immediately.
If the character is killed by the weapon, then the excess damage is calculated and expressed as overkill.

1 Like

Goodness, thank you so much! I haven’t done any balancing at all yet so that isn’t something I would have even stumbled into for a very long time. I would have been obsessing over why this was happening for so long. I will implement this fix tomorrow and close this thread assuming it works, and I don’t know why it wouldn’t if you tested it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms