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

GameMaker [RESOLVED] Space Rock, setting a "ceiling" for speed, and reducing speed over time

E

Erutan Egaro

Guest
(I am french, excuse the potential grammar errors)

Hi, I recently installed GMS2 and I went through the tutorial videos to learn how to make Space Rock with the GML. Before moving on to other videos, I wanted to polish the controls a little bit, because as good as the tutorial is, I wasn't satisfied with the clumsiness of the controls I got.

To reach this goal, my biggest issue was to create a speed "ceiling", so that the ship wouldn't go to light speed each time I held the forward key more than 3 seconds, I also wanted to reduce the speed over time so that the ship would stop if he was left without any acceleration for a time.

Here is my code for the ship object controls, those controls are in a step event. I use an azerty keyboard, those controls are mapped for ZQSD:
Code:
if(keyboard_check(ord("Q"))){
    image_angle = image_angle+5;
}

if(keyboard_check(ord("D"))){
    image_angle = image_angle-5;
}


if(keyboard_check(ord("Z"))){
        motion_add (image_angle, 0.05);
}

if(keyboard_check(ord("S"))){
    {
        motion_add (image_angle, -0.05);
    }
}

I tried to toy with the speed variable so far, and I haven't really been able to do anything useful.
I tried to use functions like
Code:
if(speed<= 8) { speed -=5;}
which worked. But once the ship reached 0 speed, the program continued to reduce it, leading to some sick reverse and an uncontrollable ship.

Does anyone has a solution? Thanks in advance.
 
You can just clamp the speed.
GML:
// direction_is_forward checks if you are moving forward

if (direction_is_forward) {
    speed = clamp(speed, 0, max_speed);
} else {
    speed = clamp(speed, -max_speed, 0);
}
 
E

Erutan Egaro

Guest
You can just clamp the speed.
GML:
// direction_is_forward checks if you are moving forward

if (direction_is_forward) {
    speed = clamp(speed, 0, max_speed);
} else {
    speed = clamp(speed, -max_speed, 0);
}

Yep it worked thanks, I just put a clamp in the step event so that the speed would stay between -5 and 5, here is the new code for anyone with the same problem
Code:
if(keyboard_check(ord("Q"))){
    image_angle = image_angle+5;
}

if(keyboard_check(ord("D"))){
    image_angle = image_angle-5;
}


if(keyboard_check(ord("Z"))){
        motion_add (image_angle, 0.05)
}

if(keyboard_check(ord("S"))){
    {
        motion_add (image_angle, -0.05);
    }
}

speed = clamp(speed, -5, 5); //can be placed here to have a object-wide ceilling
 
Top