Comments and answers for "Damage drop-off over distance on a physical projectile"
http://answers.unity.com/questions/1310378/damage-drop-off-over-distance-on-a-physical-projec.html
The latest comments and answers for the question "Damage drop-off over distance on a physical projectile"Comment by alankemp on alankemp's answer
http://answers.unity.com/comments/1312322/view.html
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.Sun, 12 Feb 2017 18:39:02 GMTalankempComment by VortexDEV on VortexDEV's answer
http://answers.unity.com/comments/1311728/view.html
This seems to work quite well, but i'm having some problems actually determining 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!Sat, 11 Feb 2017 04:46:42 GMTVortexDEVAnswer by alankemp
http://answers.unity.com/answers/1311445/view.html
You can use [Mathf.Lerp][1] 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);
}
[1]: https://docs.unity3d.com/ScriptReference/Mathf.Lerp.htmlFri, 10 Feb 2017 14:05:38 GMTalankempComment by VortexDEV
http://answers.unity.com/comments/1311292/view.html
Bump (due to approval time)Fri, 10 Feb 2017 04:00:52 GMTVortexDEV