K
K
Kydesn1k2022-01-30 11:27:24
Lua
Kydesn1k, 2022-01-30 11:27:24

How to make a gradual increase in damage for the absence of N-ovo health?

I'm not very good at math, so I don't really understand how to implement the solution.

I need to do this:
The less health the player has, the more damage he will do.
In this case, maxMulty must be involved.

Whatever it is, it would not be a banal check through if

if hp < 24 then
 damage * 4
end


Let's say:
Damage - 100
Health 24
Max multiplier 4

local maxMulty = 4
local damage = 100
local hp = 24
local maxHP = 100
local percHP = hp / maxHP --Получение процента от хп

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dollar, 2022-01-30
@Kydesn1k

Let's add an extra values ​​for convenience of presentation:

local startDamage = damage -- Начальное значение на отрезке
local endDamage = damage * maxMulty --Конечное значение

So we have startDamage, endDamage and percHP. We need to scale the damage value from initial to final, depending on the percentage. I show on my fingers how to get to the formula myself.
  • Obviously, when , we take startDamage.percHP==1
  • Obviously, when , we take endDamage.percHP==0
  • What to take, if ? Obviously the arithmetic mean. That is , or what is the samepercHP==0.5(startDamage + endDamage)/2startDamage/2 + endDamage/2

And here you can already see a hint of what the formula will be. At 50% we take half of the start value and half of the end value. For 60%, obviously, you need to take 40% of the initial and 60% of the final.
So, the formula is:
startDamage * percHP + endDamage * (1 - percHP)
You can, of course, substitute your variables here now, but I do not advise you to do this, since in its current form the formula is as simple and clear as possible.
In the programming world, a linear interpolation function is usually called lerp , although this is a matter of taste. It is important here that it is not related to xp, but is written in a general form (slightly adapted to this issue):
local function lerp(start_val, end_val, perc)
  return start_val * perc + end_val * (1 - perc) 
end

If this function is declared, then it will be possible to write below:
lerp(damage, damage * maxMulty, percHP)
If the function is declared globally, then it will be possible to use it in interrupts anywhere in the project. Globally declared without the word local :
function lerp(start_val, end_val, perc)
  return start_val * perc + end_val * (1 - perc) 
end

Examination:
print(lerp(100,400,0.9)) -- 130

S
Saboteur, 2022-01-31
@saboteur_kiev

max_health = 100
current_health = 24
max_damage_multiplier = 4
get the current health percentage (current_health / max_health)
flip it to get the increase from lesser health (1 - current_health / max_health)
multiply the maximum possible multiplier by the resulting percentage, and get:

current_damage_multiplier = max_damage_multiplier * ( 1 - current_health / max_health )

then you just multiply the damage by the multiplier
damage = damage * current_damage_multiplier
You don't need to store the percentage of health separately, if there is a max and a current one, you can always calculate it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question