I want to make the object accelerate when pressing arrows keys and decelerate until a complete stop when stop pressing. I am able to code the acceleration and deceleration part but after stop pressing there still deceleration speed left so the object keeps on drifting on it own to the right. I tried to limit the speed: after stop pressing the button, there going to be an incrementation each frame to 6, and it limit the speed to 0, 0.Here are the codes:
Create:
hspd = 0;vspd = 0;acceleration = 0.2;deceleration = -0.5;max_movespeed = 30;time_count = 0Steps:
//// Horizontal movement , acceleration and decelerationif (!keyboard_check(vk_right)and hspd > 0){ hspd += deceleration;}else if keyboard_check(vk_right){ hspd += acceleration;}if (!keyboard_check(vk_left)and hspd < 0){ hspd -= deceleration;}else if keyboard_check(vk_left){ hspd -= acceleration;}//// Limiting max horizontal movespeed and limit deceleration to 0 to stop driftingif keyboard_check(vk_right) or keyboard_check(vk_left){ hspd = clamp(hspd, -max_movespeed, max_movespeed);}else if (!keyboard_check(vk_right) and !keyboard_check(vk_left)){ time_count += 1 if time_count == 6 { hspd = clamp(hspd, 0, 0) }}//// Updating x and yx += hspd;y += vspd;I want the object to stop drifting and make the object deceleration and after 6 frames past, it comes to a complete stop. It's normal for the acceleration to be a bit longer. the value of acceleration and deceleration in the code isn't the right one, I put it so I can see the effect clearly. Otherwise they should be acceleration = 5 and deceleration = 10. The ratio of them both should be 1:2.