Is 1 the default amount of damage for the "currentHitPoints --; "?

To phrase it differently: How did we set the damage of the ballista bolt to 1 health point? Also, how can we modify the damage?

1 Like

In the EnemyHealth.cs script, we subtract 1 from the health when the projectile hits. currentHitPoints-- decreases the value by 1.

If you want to modify the damage you could do the simple thing and just add a serialised value to EnemyHealth and subtract that instead

[SerializeField] int projectileDamage = 2;

void ProcessHit() 
{
    currentHitPoints -= projectileDamage;

    if(currentHitPoints <= 0)
    {
        gameObject.SetActive(false);
        maxHitPoints += difficultyRamp;
        enemy.RewardGold();
    }
}

But EnemyHealth is not really the correct place to put it. I believe (not 100% sure) the GameObject that is passed into the OnParticleCollision is the tower (ballista) that shot this particular particle, so you should be able to retrieve the Tower class from it and if you set the damage on that, you can retrieve it. It should allow you to set damage per tower. So you could have ballistas doing 2 damage and trebuchets (for example) doing 5 damage.

What you’ll do is add the projectileDamage to the Tower script instead and create an ‘getter’ method

public class Tower : MonoBehaviour
{
    [SerializeField] int projectileDamage = 1;

    public int GetProjectileDamage()
    {
        return projectileDamage;
    }
}

Then, in the EnemyHealth script, you can retrieve the Tower and get it’s damage value

void OnParticleCollision(GameObject other) 
{
    // Try to get the tower
    if (other.TryGetComponent<Tower>(out Tower tower))
    {
        // if we got a tower, pass its damage value to process the hit
        ProcessHit(tower.GetProjectileDamage());
    }
}

void ProcessHit(int damage) // here, we now accept the damage amount
{
    currentHitPoints -= damage;

    if(currentHitPoints <= 0)
    {
        gameObject.SetActive(false);
        maxHitPoints += difficultyRamp;
        enemy.RewardGold();
    }
}
2 Likes

Thank you very much for this detailed answer!

1 Like

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

Privacy & Terms