Damage based on distance working opposite of intended

I’m making a custom explosion and it’s supposed to deal more damage the closer you are to the explosion point but for me it does the opposite, it deals more damage the further away you are from it. How can I make it work as intended, here’s my code:

local RADIUS = 50 -- damage distance from explosion point
local DEFAULT_DAMAGE = 30 -- max damage it can reach

local distanceBetween = (character.Position - rayResult.Position).Magnitude
if distanceBetween < RADIUS then
	local damageDone = DEFAULT_DAMAGE * distanceBetween / RADIUS -- the damage calculation which doesn't work properly
end

I looked on devforum for similar issues and none of the solutions helped me

1 Like

where are you referencing the damage variable?

My bad forgot to edit that part of the code, damage is DEFAULT_DAMAGE

local damageDone = DEFAULT_DAMAGE * (1 - (distanceBetween/RADIUS))

2 Likes

Oh nevermind, I see the issue with your calculation. Let’s say you were 5 studs away: 30 * 5 / 50 = 3 damage
Now if you were 10 studs away: 30 * 10 / 50 = 6. Your math is wrong.

1 Like

Yep, that’s the correct calculation. Try to use it and see if it works.

1 Like