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);
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
and then in the step event
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
step