Damage range for grenade action

I was intrigued by your comment about creating a damage range for the grenade. Here is my solution:

if(collider.TryGetComponent<Unit>(out Unit targetUnit))
{
        float damageDistance =
                 Vector3.Distance(collider.transform.position, _targetPosition) /  DAMAGE_RADIUS;

        int rangedDamage =  Mathf.RoundToInt(Mathf.Lerp(30f, 20f, damageDistance) ;

        targetUnit.Damage(rangedDamage);

        Debug.Log(rangedDamage);
}

When the grenade lands on the same grid position as the unit the unit receives the max damage but units farther away but within range receive less.

3 Likes

Great job!
Only change I would make would be rename “damageDistance” to “damageDistanceNormalized” since that variable is storing the normalized distance instead of distance in world units.

Oh, I also implemented damage based on distance

    private int CalculateDamageBasedOnDistance(Vector3 explosionPosition, Vector3 victimPosition)
    {
        var distFromGroundZero = Vector3.Distance(victimPosition, explosionPosition);
        var damage = _maxDamage - Mathf.RoundToInt(_maxDamage * (distFromGroundZero / _blastRadius));
        return damage;
    }

Your use of the Lerp is clever, though. Well done

2 Likes

Privacy & Terms