GameMaker Studio 2 Car Movement

295 Views Asked by At

I'm making a racing game for school, but I'm stuck on the car movement script. I tried to get ChatGPT to make the code for it, but it started bugging graphics out. If it's helpful, the event is attached to objPlayerCar If it's possible as well, could someone simplify it?

Here's the current code in the Step event:

// Car Movement Script

// Define movement variables
var acceleration = 0.8; // Acceleration rate
var deceleration = 0.3; // Deceleration rate
var maxSpeed = 5; // Maximum speed
var minSpeed = -2; // Minimum speed
friction = 0.1; // Friction rate

// Check for input and update speed accordingly
if (keyboard_check(ord("S"))) {
   speed += acceleration;
}
else if (keyboard_check(ord("W"))) {
   speed -= acceleration;
}
else {
   if (speed > 0) {
      speed -= deceleration;
      if (speed < 0) speed = 0;
   }
   else if (speed < 0) {
      speed += deceleration;
      if (speed > 0) speed = 0;
   }
}

// Apply friction to slow down the car
if (speed > 0) {
   speed -= friction;
   if (speed < 0) speed = 0;
}
else if (speed < 0) {
   speed += friction;
   if (speed > 0) speed = 0;
}

// Limit the speed to the maximum and minimum values
if (speed > maxSpeed) speed = maxSpeed;
if (speed < minSpeed) speed = minSpeed;


// **ADD TURNING**



// Move the car in the direction it is facing
x += lengthdir_x(speed, direction);
y += lengthdir_y(speed, direction);
1

There are 1 best solutions below

0
PotatoRock On

I have come up with something. first, make sure that the origin point of the car sprite is middle centre, then put this in the create event

/*
    I have put all of the variables like the speed in the create event,
    so that the variable is set when the object is created, and not every frame.
    also, make sure the that for car's sprite the origin is middle centre and
    the collsion type is rectangle with rotation
*/



//feel free to edit these variables
accelerationSpeed = 0.8; //how fast the car will accelerate
decelerationSpeed = 0.3; //how fast the car will decelerate
maxSpeed = 5; //maximum speed
minSpeed = -2; //minimum speed
turnSpeed = 3; //how fast the car will turn
friction = 0.1; //friction


//copy the following code if you want collisions

collision = objCol; /*here I have put the object the car collides with in a variable,
so you can change the object that the car collides with.
You can just make an object with a sprite that is just a square.
If you want to make the collisions look proper you should untick visible on the collision object,
and put a sprite on an asset layer, or a tileset, where the collision object is in the room.
The visible toggle is right under where you choose the collsion mask
*/
bounceDistance = 5; //how far you want to bounce back if you collide

and then in the step event

//getting the input and just storing in a variable, you don't have to do this, it just makes the code more readable and is quicker to write.
keyAccelerate = keyboard_check(ord("W"));
keyDecelerate = keyboard_check(ord("S"));
keyTurnLeft = keyboard_check(ord("A"));
keyTurnRight = keyboard_check(ord("D"));

var _lastDir = direction; //this will just have the previous direction, you don't need this if you're not doing collisions

//code for turning the car

//this part will change the direction of the car
if(keyTurnLeft) direction += turnSpeed; //changes the car's direction
if(keyTurnRight) direction -= turnSpeed; //changes the car's direction
image_angle=direction; //this line will just make the car sprite turn

//now that we have set the direction of the car we need to be able to move the car
if(keyAccelerate) speed += accelerationSpeed; //accelerate
if(keyDecelerate) speed -= decelerationSpeed; //decelerate
speed = clamp(speed,minSpeed,maxSpeed); //now we need to stop the car from going to fast, by using the clamp function.



//Copy following code if you want collsions
if(place_meeting(x+hspeed,y+vspeed,collision)) //checks for a collision with the collsion object
{
    speed=0;//stops the car from moving
}


/*
this probably isn't the best way of doing this,
but the the code below will just you the bounceDistance
variable, and use it when the car is turning next to a wall.
If this wasn't here it would get stuck in walls.
There would be a better way though, because this does look a bit buggy,
but it works and I couldn't find another way.
*/
if(_lastDir!=direction)
{
    if(place_meeting(x-1,y,collision)) x+=bounceDistance;
    if(place_meeting(x+1,y,collision)) x-=bounceDistance;
    if(place_meeting(x,y-1,collision)) y+=bounceDistance;
    if(place_meeting(x,y+1,collision)) y-=bounceDistance;
}

I have also allowed for the car to collide with walls. I have commented where the collision code starts, so if you don't want my method of collsions, you can just delete that code.

also here is the code without comments, because I have put quite a lot of comments in.

create

accelerationSpeed = 0.8;
decelerationSpeed = 0.3;
maxSpeed = 5;
minSpeed = -2;
turnSpeed = 3;
friction = 0.1;

collision = objCol;
bounceDistance = 5;

step

keyAccelerate = keyboard_check(ord("W"));
keyDecelerate = keyboard_check(ord("S"));
keyTurnLeft = keyboard_check(ord("A"));
keyTurnRight = keyboard_check(ord("D"));

var _lastDir = direction;

if(keyTurnLeft) direction += turnSpeed;
if(keyTurnRight) direction -= turnSpeed;
image_angle=direction;

if(keyAccelerate) speed += accelerationSpeed;
if(keyDecelerate) speed -= decelerationSpeed;
speed = clamp(speed,minSpeed,maxSpeed);



if(place_meeting(x+hspeed,y+vspeed,collision))
{
    speed=0;
}

if(_lastDir!=direction)
{
    if(place_meeting(x-1,y,collision)) x+=bounceDistance;
    if(place_meeting(x+1,y,collision)) x-=bounceDistance;
    if(place_meeting(x,y-1,collision)) y+=bounceDistance;
    if(place_meeting(x,y+1,collision)) y-=bounceDistance;
}