# Player Keeps Walking Right?

T

#### TheJJGamer

##### Guest
Heya!
I'm so pissed at this because I can't figure out what's causing it! I've been working on a Super Mario Bros. 3 clone and I'm trying to have the player slow down to a stop instead of just instantly stopping (like in the game), but something's keeping the player slowly walking to the right once he reaches the speed limit! I know it's got something to do with the slowing down when the left/right keys are released. Any help?

Code:
``````// Speed limit //
if (hspeed > speedCap) {hspeed = speedCap;}
if (hspeed < -speedCap) {hspeed = -speedCap;}

// Left/right movement //
//Keys pressed
if (keyboard_check(key_Right)) {hspeed += 0.22;}
if (keyboard_check(key_Left)) {hspeed -= 0.22;}
//Keys release
if (!keyboard_check(key_Left)) and (!keyboard_check(key_Right)) {
{if (hspeed > 0) {hspeed -= 0.22;}}
{if (hspeed < 0) {hspeed += 0.22;}}
}``````

#### NicoDT

##### Member
Hey! As you said, the problem seems to be when keys are released. What is the speedCap? I'm guessing it's not a multiple of 0.22
If it's not, then when not pressing left or right, hspeed is going to become less than 0 , and inmediately is going to gain 0.22 (last part,
if (hspeed < 0) {hspeed += 0.22;})

Try this code and let me know
Code:
``````// Speed limit //
if (hspeed > speedCap) {hspeed = speedCap;}
if (hspeed < -speedCap) {hspeed = -speedCap;}

// Left/right movement //
//Keys pressed
if (keyboard_check(key_Right)) {hspeed += 0.22;}
if (keyboard_check(key_Left)) {hspeed -= 0.22;}
//Keys release
if (!keyboard_check(key_Left)) and (!keyboard_check(key_Right)) {
if (hspeed > 0) {
hspeed -= 0.22
if (hspeed < 0) hspeed = 0
}
if (hspeed < 0) {
hspeed += 0.22
if (hspeed > 0) hspeed = 0
}

}

[/ CODE]``````

M

#### Multimagyar

##### Guest
probably the speed you set cannot be devided by 0.22 and tuss can't set it perfectly to zero so it throws it between two values like 0.18 then it takes out 0.22 it makes it -0.04 and then it since it's smaller than 0 it also adds 0.22 so it goes back to like 0.18
The easiest way to avoid this is to check if the value is in between -0.22 and 0.22 and set it to zero. it won't matter in a visual perspective because from there you only one step away from it anyway.

something like

if (hspeed<0.23 and hspeed>-0.23)
{
hspeed=0;
}

edit.: (dem ninja'd)
should be what you need.

A

#### Aura

##### Guest
Code:
``````hspeed = clamp(hspeed, -speedCap, speedCap);
//Movement
hspeed = max(abs(hspeed) - 0.22, 0) * sign(hspeed);``````
That's a simpler way of doing it.

T

#### TheJJGamer

##### Guest
Hey! As you said, the problem seems to be when keys are released. What is the speedCap? I'm guessing it's not a multiple of 0.22
If it's not, then when not pressing left or right, hspeed is going to become less than 0 , and inmediately is going to gain 0.22 (last part,
if (hspeed < 0) {hspeed += 0.22;})

Try this code and let me know
Code:
``````// Speed limit //
if (hspeed > speedCap) {hspeed = speedCap;}
if (hspeed < -speedCap) {hspeed = -speedCap;}

// Left/right movement //
//Keys pressed
if (keyboard_check(key_Right)) {hspeed += 0.22;}
if (keyboard_check(key_Left)) {hspeed -= 0.22;}
//Keys release
if (!keyboard_check(key_Left)) and (!keyboard_check(key_Right)) {
if (hspeed > 0) {
hspeed -= 0.22
if (hspeed < 0) hspeed = 0
}
if (hspeed < 0) {
hspeed += 0.22
if (hspeed > 0) hspeed = 0
}

}

[/ CODE]``````
That fixed it! I don't know why I didn't think of that from the beginning, lol. Thank you!

probably the speed you set cannot be devided by 0.22 and tuss can't set it perfectly to zero so it throws it between two values like 0.18 then it takes out 0.22 it makes it -0.04 and then it since it's smaller than 0 it also adds 0.22 so it goes back to like 0.18
The easiest way to avoid this is to check if the value is in between -0.22 and 0.22 and set it to zero. it won't matter in a visual perspective because from there you only one step away from it anyway.

something like

if (hspeed<0.23 and hspeed>-0.23)
{
hspeed=0;
}

edit.: (dem ninja'd)
should be what you need.
Wow, I never thought about that. My speed cap is set as 2, so that makes sense. I'll change it to a multiple of 0.22 just to be safe. Thanks!

Code:
``````hspeed = clamp(hspeed, -speedCap, speedCap);
//Movement
hspeed = max(abs(hspeed) - 0.22, 0) * sign(hspeed);``````
That's a simpler way of doing it.
This works too and keeps the script a lot cleaner! Thanks so much!

Thanks everybody for the help! I really appreciate it! =D