Earlier, I would find that since the character continues punching even after the target is dead, it would reward experience multiple times. The way I dealt with that was:
public void TakeDamage(in GameObject instigator, in float damage)
{
// Start when we aren't dead
if (!dead)
{
// This will call DieOrResurrect(), setting "dead" to true if necessary
Hp -= damage;
// We might be dead now!
AwardXp(instigator);
}
}
private void AwardXp(GameObject instigator)
{
if (dead)
{
instigator.GetComponent<Experience>()?.AwardXp(stats.XpReward);
}
}
Where the Hp
property is defined as
private float Hp
{
get => hitPoints;
set
{
hitPoints = Mathf.Max(value, 0);
DieOrResurrect();
}
}
… and DieOrResurrect()
works like
private void DieOrResurrect()
{
if (hitPoints <= 0 && !dead)
{
dead = true;
collider.enabled = false;
animator.SetTrigger("die");
actionScheduler.CancelCurrentAction();
}
else if (hitPoints > 0 && dead)
{
dead = false;
collider.enabled = true;
animator.SetTrigger("resurrect");
}
}
By discerning stuff from the Debugger, somehow hitting with a melee attack (if the projectilePrefab is null
) calls TakeDamage()
twice, while for projectiles, it doesn’t. So an enemy killed by a projectile doesn’t call AwardXp
since I don’t hit its dead body (yikes).
My whole repo is at https://gitlab.com/ShivamMukherjee/TL7_RPG2. Are there any corners I’m missing?