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

Fixing an angle when moving a car

P

Polybius

Guest
Hello

I am trying to recreate a Rally X game, I am very basic at Game Maker (1.4) and I'd like to know how I can spin the car to a certain angle and then keep the car facing that angle, for example say that the car starts the room facing 0° and I want to spin it to the left (180°) and once it gets there it doesn't move its angle anymore.

What I tried:

Code:
image_angle=image_angle+10;

if (image_angle==180)
{
    image_angle=180;
}
direction= image_angle;

But as I press left it keeps spinning.

Thanks for any support.
 
Last edited by a moderator:
E

elliotcoy

Guest
Hi Polybius, just tested the same code with the same result (yay consistency) and then fixed it.
Here is the fix:
Code:
image_angle=image_angle+10;

if (image_angle>=180)
{
    image_angle=180;
}

direction= image_angle;
or, even better:
Code:
if(image_angle < 180){
    image_angle = clamp(image_angle+10, 0, 180); // add 10, don't go above 180 (clamp)
}
direction = image_angle;
The reason you code broke was because when it reached 180 it would add 10, becoming 190, and then check for image_angle==180.
 
P

Polybius

Guest
Great I will check this later, probably I will have to add a Step event to check the position the car is facing and then have it moving around (I'd forgotten about the clamp() function and I was thinking about snapping.) Again, thanks!
 
Top