Vehicle driving basics
In Project 04, you spawned an empty car. But in real GTA V missions, cars don't sit empty: chauffeurs pick you up, police cruisers pursue suspects, and getaway drivers race through traffic. In this final technical lesson of Stage 4, you learn how to put an NPC behind the wheel and command them to navigate the city to a destination.
Boarding a vehicle: EnterVehicle
Never teleport an NPC into a driver seat unless it is part of an instant cutscene. The engine's immersion comes from watching the character open the door, climb in, and start the engine:
using GTA;
using GTA.Math;
// Command driver to enter vehicle with a 6-second timeout, running speed (8.0f)
driver.Task.EnterVehicle(car, VehicleSeat.Driver, 6000, 8.0f, EnterVehicleFlags.None);
Checking driver.IsInVehicle(car) tells you when the driver has sat down, closed the door, and is ready to steer.
Navigating to a destination: DriveTo
Once the driver is seated, command them to navigate the road network to a target location:
Vector3 destination = new Vector3(-802f, 175f, 72.8f);
float speedMetersPerSecond = 22.0f; // Roughly 50 MPH
VehicleDrivingFlags flags = VehicleDrivingFlags.StopForVehicles;
float arrivalRadius = 6.0f;
driver.Task.DriveTo(car, destination, speedMetersPerSecond, flags, arrivalRadius);
The game's vehicle pathfinding engine automatically follows road lanes, navigates roundabouts, negotiates traffic signals, and slows down when reaching the target radius.
Critical warning: Avoid obsolete driving flags
In older documentation and forums, you will frequently see:
// OBSOLETE in modern SHVDN: do not use
VehicleDrivingFlags flags = VehicleDrivingFlags.FollowTraffic;
VehicleDrivingFlags.FollowTraffic is obsolete in modern ScriptHookVDotNet3 and triggers compiler warnings or unpredictable steering bugs where vehicles freeze at green lights.
Instead, use modern composable driving flags:
VehicleDrivingFlags.StopForVehicles: Standard defensive driving that brakes when traffic stops ahead.VehicleDrivingFlags.Rushed: Aggressive driving that swerves through traffic and ignores red lights.VehicleDrivingFlags.AvoidVehicles: Emergency evasion.
The verified driving implementation
Here is the complete implementation matching snippet S04:
using System;
using GTA;
using GTA.Math;
public sealed class DriverDriveTo : Script
{
private Ped _driver;
private Vehicle _target;
private readonly Vector3 _destination = new Vector3(-802f, 175f, 72.8f);
public DriverDriveTo()
{
Tick += OnTick;
Aborted += OnAborted;
}
private void OnTick(object sender, EventArgs e)
{
Ped player = Game.Player.Character;
if (player == null || !player.Exists())
{
return;
}
if (_driver == null || !_driver.Exists())
{
SpawnDriverNear(player);
return;
}
if (!_driver.IsInVehicle(_target))
{
_driver.Task.EnterVehicle(_target, VehicleSeat.Driver, 6000, 8f, EnterVehicleFlags.None);
}
else
{
VehicleDrivingFlags flags = VehicleDrivingFlags.StopForVehicles;
_driver.Task.DriveTo(_target, _destination, 22f, flags, 6f);
}
}
private void SpawnDriverNear(Ped player)
{
Model model = new Model(PedHash.Cop01SMY);
if (!model.IsValid || !model.IsInCdImage || !model.Request(2000))
{
model.MarkAsNoLongerNeeded();
return;
}
try
{
_target = player.CurrentVehicle;
if (_target == null || !_target.Exists())
{
return;
}
_driver = World.CreatePed(model, _target.Position + _target.RightVector * 2f, _target.Heading);
if (_driver != null && _driver.Exists())
{
_driver.BlockPermanentEvents = true;
}
}
finally
{
model.MarkAsNoLongerNeeded();
}
}
private void OnAborted(object sender, EventArgs e)
{
if (_driver != null && _driver.Exists())
{
_driver.Delete();
}
}
}
You now possess every technical tool needed to build complete mods in GTA V. In Stage 5 Ship it, you will combine all of these systems into Project 09: Complete mod, learn how to version and package your work, and prepare for your Capstone project.