Skip to main content

Vehicle spawning

Spawning a pedestrian is forgiving: if a human spawns slightly above the sidewalk, they drop two inches onto their feet. Spawning a two-ton vehicle is not forgiving. If you spawn a car at raw coordinates, half of it might end up embedded in a lamppost, perched sideways on a guardrail, or exploding because its wheels collided inside the geometry of a building.

To spawn vehicles reliably, you must use the game's road network.

Finding a valid road: GetNextPositionOnStreet

GTA V contains an extensive internal graph of road nodes representing lanes, intersections, and street directions. Instead of guessing coordinates, ask the engine to snap your target location to the nearest valid asphalt node:

using GTA;
using GTA.Math;

// Search for a road node 25 meters ahead of the player
Vector3 searchPoint = player.Position + player.ForwardVector * 25f;
Vector3 roadPoint = World.GetNextPositionOnStreet(searchPoint);

if (roadPoint == Vector3.Zero)
{
// No road found nearby (player is on a mountain peak or rooftop)
GTA.UI.Notification.PostTicker("No valid road found nearby.", false);
return;
}

GetNextPositionOnStreet returns Vector3.Zero if no street node exists within search range. Always check for this before attempting to spawn.

The vehicle streaming ritual

Vehicle models are loaded through the same streaming manager as peds, but using VehicleHash:

Model model = new Model(VehicleHash.Dominator);
if (!model.IsValid || !model.IsInCdImage || !model.Request(2000))
{
model.MarkAsNoLongerNeeded();
return;
}

Vehicle car = null;
try
{
car = World.CreateVehicle(model, roadPoint, player.Heading);
}
finally
{
model.MarkAsNoLongerNeeded();
}

The PlaceOnGround method

When World.CreateVehicle finishes, the car sits at the exact Z altitude returned by the node. But because different vehicles have different wheel diameters and suspension heights, the tires might sit a few millimeters below the road surface.

Always settle the suspension immediately:

if (car != null && car.Exists())
{
car.IsPersistent = true;
car.PlaceOnGround();
}

PlaceOnGround() snaps the bottom of all four tires flush with the collision mesh of the asphalt. This prevents the vehicle from violently bouncing into the air on its first physics frame.

In Project 04, you put these rules together to build a vehicle spawner that cleanly delivers an empty car on demand.