• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Function Lerp

MacGregory

Member
how does the lerp function work? I want HP to fall smoothly with damage, and not abruptly in one frame! right now I wrote it like this health = health_max health = lerp (health, health_max, 0.15) at the enemy's Step. HP fall smoothly, but immediately recover to the previous level, in which I am wrong, please tell me.
 
Are both those lines of code actually in the same event? Lerp here is doing nothing because the first line already equalizes both variables.
 

FoxyOfJungle

Kazan Games
You can use an approach function to do that:

GML:
/// @func approach(val1, val2, amount)
/// @arg val1
/// @arg val2
/// @arg amount
// Moves "a" towards "b" by "amount" and returns the result

function approach(val1, val2, amount)
{
    if (val1 < val2) {
        return min(val1 + amount, val2);
    } else {
        return max(val1 - amount, val2);
    }
}
 
What's the purpose of the "health = health_max;" line being in the Step event? Surely you don't mean the health to instantly be set to the maximum every time the latter changes.
 

FrostyCat

Redemption Seeker
You want to have a fake HP variable following the real HP variable. Anything that needs a smooth transition would then reference the fake one.

Create:
GML:
health_shadow = health;
Step:
GML:
if (abs(health-health_shadow) > 1) {
    health_shadow = lerp(health_shadow, health, 0.15);
} else {
    health_shadow = health;
}
You mistake is making the real health follow the max health, instead of the fake health following the real health.
 
Top