How do i get the required damage depending on the magnitude?

Basically, what i want is to get a number depending on the magnitude from the impact center to the HumanoidRootPart

Example:

If the magnitude of the player = 100, the damage will be 1, magnitude=50 => damage=50, magnitude=1 => damage=100

You can do that by, Getting the Impact point’s Vector3 cords and player’s character’s Vector3 cords, then Subtracting Impact point Vector3 from Player’s character’s Vector3 will get a relative vector coordinates to the impact point. Then use the Magnitude property of the result vector and do if-else statement by the following:

if(resultVector.Magnitude == 100) then
    damage = 1
elseif(resultVector.Magnitude = 50) then
   damage = 50
elseif(resultVector.Magnitude == 1) then
   damage = 100
end
2 Likes

But how do I get damage depending on the magnitude if the Magnitude ~= 1, 50, 100?

Any formula or function?

1 Like

How do you want to get the damage? Like percentage drop or what?

1 Like

You can use a negative linear function that is clamped.

So y = -mx + b

Its also in the documentation with a code example

-- look for a humanoid
			local humanoid = parentModel:FindFirstChild("Humanoid")
			if humanoid then
				local distanceFactor = distance / explosion.BlastRadius -- get the distance as a value between 0 and 1
				distanceFactor = 1 - distanceFactor -- flip the amount, so that lower == closer == more damage
				humanoid:TakeDamage(maxDamage * distanceFactor) -- TakeDamage to respect ForceFields
			end

This roblox example is similar to @LakshyaK2011 idea except its in decimal percentage form 0-1 and not 0-100%

2 Likes

I assume that you want to damage player but as the magnitude increases the damage decreases.
You can just do that by:
Defining the damage radius in studs values. Then
if the player is in radius then do:
get the percentage of how much player is away from impact point’s magnitude,
then subtract the percentage from 100, and then get the total number of damage points by that calculated percent of players total hp

2 Likes

Imagine that i put it into a ability system, that doesn’t uses an explosion, but creates a part after the condition has been meeted(on the animation keyframe), then how do i get the damage?

The magnitude is counted from the created part

Thats actually a genius idea, dont know why i didnt do that earlier
Thank you!

1 Like

No problem man, that’s what makes me feel good and happy!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.