• 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!

Clamping smooth approach?

E

Edwin

Guest
Hello, people I have this script that creates smooth approaching:
Code:
var distance = target - current;

if (abs(distance_x) > 0.1) {
    current += ( target - current ) * speed;
} else {
    current = target;
}
This code makes smooth approaching but current will be faster depending of target distance. How I can just clamp the speed of it?
 
T

Taddio

Guest
If you want linear acceleration, you can use lerp(); Did you try it yet?
like current=lerp(current, target, 0.2);
 
E

Edwin

Guest
If you want linear acceleration, you can use lerp(); Did you try it yet?
like current=lerp(current, target, 0.2);
This is practically the same function. Lerp don't clamp the speed (like the code above), so I need to make my own script with speed clamping.
 
Last edited:
T

Taddio

Guest
This is practically the same function. Lerp don't clamp the speed (like the code above), so I need to make my own script with speed clamping.
Oh you mean you go faster than your max speed? If so clamp it with clamp() right after the code you have and you'll be set. Starting to doubt I understand what's the problem, hahahaha
 
E

Edwin

Guest
Oh you mean you go faster than your max speed? If so clamp it with clamp() right after the code you have and you'll be set. Starting to doubt I understand what's the problem, hahahaha
There is no max speed. You increase it with the third argument in the lerp function. Try to make a room with large width and medium height and try to put the mouse to the farthest point from the object in which you use lerp function and see the results. Then, so to speak, I need to create a max speed.
 

samspade

Member
Do you just mean this?

Code:
var distance = target - current;

if (abs(distance_x) > 0.1) {
   current += clamp(( target - current ) * speed, min_speed, max_speed);
} else {
   current = target;
}
 
E

Edwin

Guest
Do you just mean this?

Code:
var distance = target - current;

if (abs(distance_x) > 0.1) {
   current += clamp(( target - current ) * speed, min_speed, max_speed);
} else {
   current = target;
}
That clamps the speed, but it changes the value too fast.
 
Top