I'm working on a car script, and I'm having a lot of trouble figuring out how to stop the car from driving over a wall, and also being able to turn the car away from the wall and drive off again without the collision detection catching it and stopping it from moving.
Does anybody have any ideas about how to do this?
PHP Code:
//#CLIENTSIDE
const CAR_MAX = 2;
const CAR_FRI = 0.1;
const CAR_ACC = 1.2;
function onCreated() {
this.angle = 0;
client.incar = false;
onTimeout();
}
function onPlayerChats() {
if (player.chat == "car") {
client.incar = !client.incar;
player.chat = "Car:" SPC client.incar;
}
}
function onTimeout() {
if (client.incar) {
freezeplayer(0.05);
if (keydown(0)) {
this.speed = min(CAR_MAX, max(0.1, CAR_ACC * this.speed));
} else {
this.speed *= (1 - CAR_FRI);
}
if (keydown(1)) this.angle -= 0.1;
if (keydown(3)) this.angle += 0.1;
if (client.stuck == true) {
this.angle += 0.2;
}
if (player.dir == 0 || player.dir == 2) {
temp.cpos = {player.x + 0.5, player.y + 1};
if (onwall(cpos[0] - 2, cpos[1] - 1) || onwall(cpos[0] + 4, cpos[1] - 1) || onwall(cpos[0] + 4, cpos[1] + 2) || onwall(cpos[0] - 2, cpos[1] + 2)) {
//Stop from driving onto wall
} else {
player.x -= sin(this.angle) * this.speed;
player.y += cos(this.angle) * this.speed;
}
} elseif (player.dir == 1 || player.dir == 3) {
temp.cpos = {player.x + 0.5, player.y + 1};
if (onwall(cpos[0] - 3, cpos[1] - 1.5) || onwall(cpos[0] + 6, cpos[1] - 1.5) || onwall(cpos[0] + 6, cpos[1] + 3.5) || onwall(cpos[0] - 2, cpos[1] + 3.5)) {
//Stop from driving onto wall
} else {
player.x -= sin(this.angle) * this.speed;
player.y += cos(this.angle) * this.speed;
}
}
player.dir = getdir(-sin(this.angle), cos(this.angle));
}
setTimer(0.05);
}
(Pardon the messy code. I'm yet to clean it up since I'm still working on it)
Also, is there a simpler method of checking for walls rather than checking for walls by calling onwall() a gazillion times?