Advertisement
Antthony_12

Untitled

Jun 12th, 2025
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | Gaming | 0 0
  1. using System;
  2. using GTA;
  3. using GTA.Native;
  4. using GTA.Math;
  5.  
  6. public class AutoHandbrake : Script
  7. {
  8.     // Indicates whether the handbrake is active
  9.     private bool _handbrakeActive = false;
  10.  
  11.     // Controls the cooldown time between key presses to prevent multiple activations
  12.     private bool _keyPressCooldown = false;
  13.  
  14.     // List of allowed vehicle classes for using the automatic handbrake
  15.     private readonly string[] ALLOWED_VEHICLE_CLASSES = {
  16.         "Compacts", "Sedans", "SUVs", "Coupes", "Muscle",
  17.         "Sports", "Super", "Motorcycles", "OffRoad", "Industrial",
  18.         "Utility", "Vans", "Commercial", "Emergency", "Military",
  19.         "OpenWheel", "Service", "SportsClassics"
  20.     };
  21.  
  22.     // Constructor: subscribes the OnTick method to the Tick event
  23.     public AutoHandbrake()
  24.     {
  25.         Tick += OnTick;
  26.     }
  27.  
  28.     // Method executed on every game tick
  29.     private void OnTick(object sender, EventArgs e)
  30.     {
  31.         // Gets the player's character
  32.         Ped player = Game.Player.Character;
  33.        
  34.         // If the player is not in a vehicle and the handbrake is active, deactivate it
  35.         if (!player.IsInVehicle() && _handbrakeActive)
  36.         {
  37.             _handbrakeActive = false;
  38.             return;
  39.         }
  40.  
  41.         // Handles toggling the handbrake on or off
  42.         HandleHandbrakeToggle(player);
  43.  
  44.         // Applies the current handbrake state to the vehicle
  45.         ApplyHandbrakeState(player);
  46.     }
  47.  
  48.     // Handles the logic for toggling the handbrake
  49.     private void HandleHandbrakeToggle(Ped player)
  50.     {
  51.         // Checks if the 'N' key is pressed, there is no cooldown, and the player is in a vehicle
  52.         if (Game.IsKeyPressed(System.Windows.Forms.Keys.N) && !_keyPressCooldown && player.IsInVehicle())
  53.         {
  54.             Vehicle vehicle = player.CurrentVehicle;
  55.            
  56.             // If the vehicle is valid and nearly stopped, toggle the handbrake state
  57.             if (vehicle != null && IsValidVehicle(vehicle) && vehicle.Speed < 0.1f)
  58.             {
  59.                 _handbrakeActive = !_handbrakeActive;
  60.             }
  61.  
  62.             // Activates the cooldown to prevent rapid multiple activations
  63.             _keyPressCooldown = true;
  64.             Wait(500); // Waits for 500 ms
  65.             _keyPressCooldown = false;
  66.         }
  67.     }
  68.  
  69.     // Applies the handbrake state to the current vehicle
  70.     private void ApplyHandbrakeState(Ped player)
  71.     {
  72.         // If the player is not in a vehicle, do nothing
  73.         if (!player.IsInVehicle()) return;
  74.  
  75.         Vehicle vehicle = player.CurrentVehicle;
  76.  
  77.         // If the vehicle is null or the player is not the driver, do nothing
  78.         if (vehicle == null || vehicle.Driver != player) return;
  79.  
  80.         if (_handbrakeActive)
  81.         {
  82.             // Stops the vehicle and activates the handbrake
  83.             vehicle.Velocity = Vector3.Zero;
  84.             Function.Call(Hash.SET_VEHICLE_ENGINE_ON, vehicle, true, true, false);
  85.             Function.Call(Hash.SET_VEHICLE_HANDBRAKE, vehicle, true);
  86.         }
  87.         else
  88.         {
  89.             // Deactivates the handbrake
  90.             Function.Call(Hash.SET_VEHICLE_HANDBRAKE, vehicle, false);
  91.         }
  92.     }
  93.  
  94.     // Checks if the vehicle belongs to an allowed class
  95.     private bool IsValidVehicle(Vehicle vehicle)
  96.     {
  97.         // Gets the vehicle's class and checks if it is in the list of allowed classes
  98.         string vehicleClass = Enum.GetName(typeof(VehicleClass), vehicle.ClassType);
  99.         return Array.Exists(ALLOWED_VEHICLE_CLASSES, allowedClass => allowedClass == vehicleClass);
  100.     }
  101. }
Tags: gtav
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement