So I have a variable called MinDamage and it's equal to 2. I have another variable called MaxDamage wich is equal to 6. And I have another variable for the maximum fall off distance wich is set to 90. How woul I make it so that the damage is ranged based? So if were to shoot in between the maximum fall off distance, the damage would be 2 - 6, and if I'm farther than 90 studs it would just set the damage to 6?
How do I make bullet damage fall off?
2 answers
So you could do this as a percentage.
We know that our total change in damage is up to 4, but for efficiency lets do this:
local BulletMaxDelta = MaxDamage-MinDamage
So we know that from 0 studs away we should be doing maximum damage (at least I assume that's what you mean.)
Given that we can get the range, I'll let you handle the specifics but for now I'll simply call it a variable range
our 90 studs would be 0% bullet delta based on minimum damage and 100% bullet delta based on maximum damage.
So for simplicity we'll create new variable which is anything less than 90.
local AdjustedRange
if range > MaxFalloff then
AdjustedRange = MaxFalloff
else
AdjustedRange = range
end
Since we know the adjusted range is now accurate we can say local PercentDelta = AdjustedRange/MaxFalloff
Knowing this we can make our damage adjustment:
local DamageAdjustment = PercentDelta*BulletMaxDelta
And since I built this relative to max damage our final change is to subtract this from the full damage.
local FinalDamage = MaxDamage-DamageAdjustment
If we subtract one Vector3 from another, and then get .magnitude, we find the distance between two points. From there it's just a bit of maths to get what we want.
Note that this means you'll have to somehow keep track of the start position of a projectile if you're using projectile based weapons.
--[[
Calculates falloff damage based on the start and end positions
startPosition: Vector3 -> Where the weapon was fired from
endPosition: Vector3 -> Where the weapon hit its target
minimumDamage: Numeric -> The minimum damage the weapon will do
maximumDamage: Numeric -> The maximum damage the weapon will do
minimumFalloffDistance: Numeric -> The distance before we start applying
falloff. Hits shorter than this distance will always do maximum damage
maximumFalloffDistance: Numeric -> The distance before we stop applying falloff.
Hits further than this distance will always do minimum damage
]]
function calculateFalloffDamage(
startPosition,
endPosition,
minimumDamage,
maximumDamage,
minimumFalloffDistance,
maximumFalloffDistance)
local distance = (endPosition - startPosition).magnitude
if distance < minimumFalloffDistance then
return maximumDamage
elseif distance > maximumFalloffDistance then
return maximumDamage
else
local falloffRange = maximumFalloffDistance - minimumFalloffDistance
local percentage = (distance - minimumFalloffDistance) / falloffRange
return maximumDamage * percentage
end
end
This discussion is preserved as it was. New answers, comments, and votes are closed.
