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
Answer by alankemp · Feb 10, 2017 at 02:05 PM
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);
}
This seems to work quite well, but i'm having some problems actually deter$$anonymous$$ing how far the projectile has travelled. I've tried the basic Distance = Velocity * Time formula using the initial velocity of the projectile, and Time.time, but this doesn't seem to give accurate results. I'm currently calculating it by having an initial starttime variable that's set when the object is instantiated, and calculating the distance travelled on collision. It doesn't seem to work, sometimes it works, most of the time it doesn't, and it tends to give weird results, here's the code i'm using for it
private float startTime;
void Awake(){
startTime = Time.time;
}
void OnCollisionEnter(Collision collision) {
distanceTravelled = 400 * (Time.time - startTime);
}
Any suggestions on accurately finding the distance the projectile has travelled?
Thanks!
You know where the bullet started (where you spawned it), and you know where it hit.
You can use Vector3.Distance to work out the distance between two points.