Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using GTA;
- using GTA.Native;
- using GTA.Math;
- public class AutoHandbrake : Script
- {
- // Indicates whether the handbrake is active
- private bool _handbrakeActive = false;
- // Controls the cooldown time between key presses to prevent multiple activations
- private bool _keyPressCooldown = false;
- // List of allowed vehicle classes for using the automatic handbrake
- private readonly string[] ALLOWED_VEHICLE_CLASSES = {
- "Compacts", "Sedans", "SUVs", "Coupes", "Muscle",
- "Sports", "Super", "Motorcycles", "OffRoad", "Industrial",
- "Utility", "Vans", "Commercial", "Emergency", "Military",
- "OpenWheel", "Service", "SportsClassics"
- };
- // Constructor: subscribes the OnTick method to the Tick event
- public AutoHandbrake()
- {
- Tick += OnTick;
- }
- // Method executed on every game tick
- private void OnTick(object sender, EventArgs e)
- {
- // Gets the player's character
- Ped player = Game.Player.Character;
- // If the player is not in a vehicle and the handbrake is active, deactivate it
- if (!player.IsInVehicle() && _handbrakeActive)
- {
- _handbrakeActive = false;
- return;
- }
- // Handles toggling the handbrake on or off
- HandleHandbrakeToggle(player);
- // Applies the current handbrake state to the vehicle
- ApplyHandbrakeState(player);
- }
- // Handles the logic for toggling the handbrake
- private void HandleHandbrakeToggle(Ped player)
- {
- // Checks if the 'N' key is pressed, there is no cooldown, and the player is in a vehicle
- if (Game.IsKeyPressed(System.Windows.Forms.Keys.N) && !_keyPressCooldown && player.IsInVehicle())
- {
- Vehicle vehicle = player.CurrentVehicle;
- // If the vehicle is valid and nearly stopped, toggle the handbrake state
- if (vehicle != null && IsValidVehicle(vehicle) && vehicle.Speed < 0.1f)
- {
- _handbrakeActive = !_handbrakeActive;
- }
- // Activates the cooldown to prevent rapid multiple activations
- _keyPressCooldown = true;
- Wait(500); // Waits for 500 ms
- _keyPressCooldown = false;
- }
- }
- // Applies the handbrake state to the current vehicle
- private void ApplyHandbrakeState(Ped player)
- {
- // If the player is not in a vehicle, do nothing
- if (!player.IsInVehicle()) return;
- Vehicle vehicle = player.CurrentVehicle;
- // If the vehicle is null or the player is not the driver, do nothing
- if (vehicle == null || vehicle.Driver != player) return;
- if (_handbrakeActive)
- {
- // Stops the vehicle and activates the handbrake
- vehicle.Velocity = Vector3.Zero;
- Function.Call(Hash.SET_VEHICLE_ENGINE_ON, vehicle, true, true, false);
- Function.Call(Hash.SET_VEHICLE_HANDBRAKE, vehicle, true);
- }
- else
- {
- // Deactivates the handbrake
- Function.Call(Hash.SET_VEHICLE_HANDBRAKE, vehicle, false);
- }
- }
- // Checks if the vehicle belongs to an allowed class
- private bool IsValidVehicle(Vehicle vehicle)
- {
- // Gets the vehicle's class and checks if it is in the list of allowed classes
- string vehicleClass = Enum.GetName(typeof(VehicleClass), vehicle.ClassType);
- return Array.Exists(ALLOWED_VEHICLE_CLASSES, allowedClass => allowedClass == vehicleClass);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement