Damage drop-off over distance on a physical projectile

Hey!

I’m currently shooting a physical projectile in my weapon controller, that uses a sphere collider and a rigidbody, and has an initial velocity of e.g. 900m/s, and i’m wondering how I would go about adding a damage drop off to my bullet. Currently my bullet has a static base damage, and that gets altered to 0.5x, 1.0x, 2.5x depending on which part of the body it hits. I’m looking for a way to create a similar system to games such as Battlefield, where you have a minimum and maximum damage, and a drop off start and drop off end distance.


For example, let’s say I have a weapon with these stats

Maximum Damage: 56

Minimum Damage: 39

Drop Off Start Distance: 36m

Drop Off End Distance: 65m


Basically, when the bullet fires, it has a damage of 56, but once it has travelled 36 metres, the damage starts progressively dropping from 56, until it travels 65 metres and drops to its lowest damage, 36. During this drop off distance, I want the damage of the bullet to progressively drop in a linear decline, rather than just setting it to the damage at that distance.

Does anyone know how I can achieve this?

Thanks, VortexDEV

You can use Mathf.Lerp to interpolate between two values. In this case, you want to lerp between your min and max damage by the amount you are in the drop off range.

        float GetDamage(float bulletDistance, float minDamage, float maxDamage, float dropOffStart, float dropOffEnd)
        {
            // Use the min/max damage if we're outside the drop off range
            if (bulletDistance <= dropOffStart) return maxDamage;
            if (bulletDistance >= dropOffEnd) return minDamage;

            // Where in the drop off range is the distance we are interested in?
            // We want this in the range of 0 at the start to 1 at the end so we can use Lerp
            float dropOffRange = dropOffEnd - dropOffStart;

            float distanceNormalised = (bulletDistance - dropOffStart) / dropOffRange;

            return Lerp(maxDamage, minDamage, distanceNormalised);
        }