Sign in to follow this  
Captien

Cruise Control

Recommended Posts

Captien    29

Hello Everyone,
 

Today I would like to introduce a Tutorial about how to do Cruise control for your vehicle.

The script generally sets your vehicle on a constant velocity without the need for you to accelerate, and you can have full control of the vehicle again by press the brakes or just run out of the car ;D (joke).

Anyways it's simple

Server-side:

Spoiler
mp.events.add('playerEnteredVehicle', (player) => {
    player.call('VehicleEnter', player.seat)
})

mp.events.add('playerExitVehicle', (player) => {
 player.call('VehicleExit', player.seat)
});

This generally 2 events that calls the client side events and tells them about if the player entered the vehicle and is a Driver or not.

Client-side:

Spoiler
let vehicle, vehSeat, vehMaxSpeed = null;
let state = 0;
let chat = false;

mp.keys.bind(0x54, true, (player) => { // If Chat was triggered
    if (!chat) {
        chat = true;
    }
});

mp.keys.bind(0x0D, true, (player) => { // If Chat was stopped.
    if (chat) {
        chat = false;
    }
});

mp.events.add('VehicleEnter', (seat) => { // Getting Vehicle Data on Player entering vehicle
    if (seat == 0) { // if he/she was driver
        vehSeat = seat;
        vehicle = mp.players.local.vehicle;
        vehMaxSpeed = mp.game.vehicle.getVehicleModelMaxSpeed(vehicle.model);
        mp.game.graphics.notify(`Press ~r~C if you want to start Cruise control.`);
    }
})

mp.events.add('VehicleExit', (seat) => { // Reseting Data when driver exits the vehicle.
    if (seat == 0) {
        state = 0;
    }

    vehicle = null;
    vehSeat = -3;
})


mp.keys.bind(0x43, true, _ => { // binding C to the cruise speed
    if (vehSeat == 0 && vehicle && !chat) { // if he/she was driver and chat isn't active.
        if (state == 0 || state == 2) { // if cruise was disabled
            state = 1;
            toggle(); // start the cruise
        } else if (state == 1) // if cruise was enabled
        {
            state = 2;
            toggle(); // stop the cruise
        }
    } 
  	 else {
        	return false;
          }
})

mp.events.add('render', () => {

    if (vehicle)
        mp.game.graphics.drawText(`${(vehicle.getSpeed() * 3.6).toFixed(0)} km/h`, 7, [255, 255, 255, 185], 0.9, 0.9, true, 0.5, 0.005); // It was made to debug the script.

    if (state == 1 && vehicle) { // if cruise was triggered
        let currentvelo = vehicle.getVelocity();

        currentvelo.x = currentvelo.x * 1.1;
        currentvelo.y = currentvelo.y * 1.1;

        vehicle.setVelocity(currentvelo.x, currentvelo.y, currentvelo.z) // set velocity to the current one.

        if (vehicle.hasCollidedWithAnything() || vehicle.isInAir()) // check if vehicle did collision or went off the ground
        {
            state = 2; // stop cruise
            toggle();
        }

        if (buttonchecker()) { // if space, enter, F, S buttons were triggered
            state = 2; // stop cruise
            toggle();
        }
    }
});

function toggle() { // Toggler of the Cruise speed.
    if (state == 1 && vehicle) {
        let speed = vehicle.getSpeed();
        vehicle.setMaxSpeed(speed);
        mp.game.graphics.notify(`Your cruise speed was set to ~b~${(speed * 3.6).toFixed(0)}~b~ km/h`);
    }


    if (state == 2 && vehicle) {
        vehicle.setMaxSpeed(vehMaxSpeed);
        mp.game.graphics.notify(`Cruise speed is now ~r~disabled! Drive safely!`);
        state = 0;
    }
}

function buttonchecker() { // if user presses space, s , enter, or F returns true
    if (mp.keys.isDown(32) === true) return true;
    if (mp.keys.isDown(83) === true) return true;
    if (mp.keys.isDown(70) === true) return true;
    if (mp.keys.isDown(13) === true) return true;

    return false;
}

 

You can also download full source Here.

 

Thank you and i wish you enjoyed the Tutorial. Wait for more soon!

Edited by Captien
Fixed some syntax errors
  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.