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

Windows How do i add movement easing?

S

SquintyP

Guest
by movement easing i mean easing character movement, when i let go of the movement key, i want the character to slowly come to a halt, not just stop immediately.

What I've been trying to do is putting a loop on the speed of the character multiplied by 0.8, slowly decreasing their speed overtime, but i haven't quite figured out how to work that. Here's the code i tried:
Create event:
spd = 12
loop = 12*0.8 while (!instance_exists(obj_char)) repeat (100) {
var
}

spdease = 0.8
key_d = ord("D");
key_a = ord("A");

Step event:
spdease = 12*0.8
if(keyboard_check(key_d)) x += spd;
if(keyboard_check(key_a)) x -= spd;
if(keyboard_check(key_d)) {
keyboard_lastkey = key_d
}
if (keyboard_check(key_a)){
keyboard_lastkey = key_a
}

if(keyboard_check(vk_nokey))and keyboard_lastkey = key_d x += loop;
if(keyboard_check(vk_nokey))and keyboard_lastkey = key_a x -= loop;

(I've just started using game maker, if you can't already tell, haha.)
 
I usually have a small value that I subtract(or add if negative speed) until we are back at 0. Which is much simpler and works very well when done right.
 

Niels

Member
Have something like this:

Create:
Spd = 0;
Max_spd = 6;
Acc = 0.5;
Decc = 1.

Step:

If keyboard_check(vk_right) {
If spd >=Max_spd {
Spd = Max_spd} else {
Spd += Acc
} else {
If spd >=0{
Spd - =Decc.
}

(typed on my phone, and from my head so could have some spelling mistakes)
 
I don't think you want "if spd >=0", that will decrease it even if it's at 0 so it will go negative.
But yeah, that's a very basic version of what I meant.
 

NightFrost

Member
If you want to decrement an arbitrary value by another but don't want it to go below certain threshold, you'd decrease the speed like:
Code:
if(Spd > 0) Spd = max(Spd - Dec, 0);
meaning if speed is above zero, it is set to higher of: speed minus deceleration, or zero.
 

w0rm

Member
This is an old thread but in case someone looks for an answer. I have been using the following. It's from tutorial but don't remember from which one.

Create event:
ground_acc = 0.05;
run_speed = 5;


Step event:
var _move = keyboard_check(vk_right) - keyboard_check(vk_left)
hspeed = ground_acc * (_move * run_speed) + (1-ground_acc) * hspeed;
 
Top