A car in the world: yours, a Drivatar's, or traffic. Read where it is, how fast it goes and what its driver is doing, push it, light its exhausts or repair it.
Thread: game thread only (Game.Tick, Game.Run, an ANNA entry), or inside Game.PhysicsStep for the car it gives you.
You don't create a Vehicle yourself. You get one from Player.Car, World.Vehicles, World.GetNearestVehicle, World.GetVehiclesInRange or Game.PhysicsStep.
Example
A small boost: while you hold the throttle down on the ground, your car is pushed forwards at 10 m/s², and its exhausts shoot flames.
protected override void OnInit()
{
Game.PhysicsStep += (car, dt) =>
{
if (car.IsPlayer && car.Throttle > 0.95f && car.IsOnGround)
car.Velocity += car.ForwardFlat * (10 * dt);
};
Game.Tick += () =>
{
var car = Player.Car;
if (car is not null) car.ExhaustFlames = car.Throttle > 0.95f;
};
}
Protected Overrides Sub OnInit()
AddHandler Game.PhysicsStep,
Sub(car As Vehicle, dt As Single)
If car.IsPlayer AndAlso car.Throttle > 0.95F AndAlso car.IsOnGround Then
car.Velocity += car.ForwardFlat * (10 * dt)
End If
End Sub
AddHandler Game.Tick,
Sub()
Dim car = Player.Car
If car IsNot Nothing Then car.ExhaustFlames = car.Throttle > 0.95F
End Sub
End Sub
Members
Properties
| Member | What it does |
|---|---|
Kind (property, get, VehicleKind) | Whose car it is: Player, Drivatar, Traffic or Other. |
IsPlayer (property, get, bool) | True if it's your car. |
ModelId (property, get, int) | The car's id, the one CarCatalog and Garage use. |
Model (property, get, CarInfo?) | Which car it is, with its names. Null if the catalogue doesn't have it. |
Position (property, get, Vector3) | Where it is, in metres. |
Forward (property, get, Vector3) | Which way it points (length 1). |
ForwardFlat (property, get, Vector3) | Which way it points, flattened to the ground (Y = 0) and of length 1. Good for pushing a car along the road without throwing it skyward. Zero when the nose points almost straight up or down. |
Velocity (property, get/set, Vector3) | How fast and which way it's moving, in m/s. You can only set it inside Game.PhysicsStep, on the car it gives you. car.Velocity += car.ForwardFlat * (10 * dt) pushes it forwards at 10 m/s². |
Speed (property, get, float) | Speed in m/s, as the car's own physics has it. |
SpeedKmh (property, get, float) | Speed in km/h. |
SpeedMph (property, get, float) | Speed in mph. |
IsOnGround (property, get, bool) | True while it's on the ground. False from when it starts to fall freely (a jump, a crest) until a moment after it lands, so a bump doesn't count as ground at once. |
IsInAir (property, get, bool) | The opposite of IsOnGround. |
Gear (property, get, int) | The gear: 0 is reverse, 1 is first, and so on. |
Rpm (property, get, float) | Engine speed in revolutions a minute. |
Throttle (property, get, float) | The throttle as the car has it this frame, 0 to 1. |
BrakePedal (property, get, float) | The brake as the car has it this frame, 0 to 1. |
Steering (property, get, float) | The steering as the car has it this frame, -1 to 1. |
Handbrake (property, get, float) | The handbrake as the car has it this frame, 0 to 1. |
HeadlightsOn (property, get/set, bool) | The headlights, on or off now (the game's own choice while they're automatic). Setting them takes them over from the game's automatic lights for this car, as ANNA does, until you set HeadlightsAutomatic back. |
HeadlightsAutomatic (property, get/set, bool) | True while the game picks the headlights (at dusk, in tunnels). Set it to true to give them back to the game. New in 0.12.0. |
Indicators (property, get/set, Indicators) | The indicators: Left, Right, Hazards or Off. Ones you set stay on until you set Off (the game would reset them now and then). New in 0.12.0. |
AiDriving (property, get/set, bool) | True while the game's AI drives the car. Set it on to hand your car to the AI driver, as ANNA's autodrive does: it drives the route the game is following (an event's, a race's, a road trip's). In free roam a GPS route needs the sat-nav's tracking started too, which this doesn't do yet. Set it off to take the car back. New in 0.12.0. |
Animations (property, get, IReadOnlyList<string>) | The car's animation events, the names PlayAnimation takes: "roof:open", "doorLF:toggle", "hood:close"... New in 0.12.0. |
Clutch (property, get, float) | The clutch pedal, 0 (out) to 1 (in). New in 0.12.0. |
RedlineRpm (property, get, float) | Where the red zone starts on the rev counter, in rpm. New in 0.12.0. |
LimiterRpm (property, get, float) | The rev limiter, in rpm. New in 0.12.0. |
Boost (property, get, float) | The turbo's boost, as the game keeps it (0 without a turbo or off boost). Its unit isn't known for sure. New in 0.12.0. |
MediaName (property, get, string) | The car's name in the game's files ("ALF_8C_08"). Any thread. New in 0.12.0. |
ExhaustFlames (property, get/set, bool) | Flames out of the exhausts, a burst every 40 ms, until you set it back to false (or the car leaves the world). The game's own backfire effect, from the car's real exhaust tips. Works on any car. On cars other than yours: ⚠️ Not tested much in the game yet. |
Handle (property, get, nint) | The game's own handle for the car, for advanced code of your own. |
Methods
| Member | What it does |
|---|---|
Backfire(float seconds = 0.05f) | Flames out of the exhausts for seconds. The default is one pop. Does nothing if ExhaustFlames is already on. |
Repair(bool simulated = true) | Repairs the car, as the game does when you enter the garage or start a road trip. The damage you see goes. With simulated true, the simulated damage goes too (what Damage & Tire Wear: Simulation does to the handling). |
DistanceTo(Vector3 point) → float | Straight-line distance to point, in metres. |
DistanceTo(Vehicle other) → float | Straight-line distance to another car, in metres. |
Equals(Vehicle? other) → bool | True if both are the same car. Equals(object?) and GetHashCode() match it, so you can use cars as dictionary keys within a frame. |
ToString() → string | The kind and short name of the car, for logs. |
PlayAnimation(string name) → bool | Plays the car's animation event name ("roof:open"), as ForzaVista does: the part moves, wherever the camera is. False if the car has no such event. New in 0.12.0. |
HasAnimation(string name) → bool | True if the car has the animation event name. New in 0.12.0. |
HasPart(CarPart part) → bool | True if the car has part to animate (a roof, pop-up lights...). New in 0.12.0. |
OpenPart(CarPart part), ClosePart(CarPart part), TogglePart(CarPart part) → bool | Opens, closes or toggles part. False if the car hasn't got it. New in 0.12.0. |
Vehicle.FromHandle(nint handle) → Vehicle? (static) | The car at handle (a ForzaCar* your own hook got), or null if it isn't a car in the world now. New in 0.12.0. |
VehicleKind
Who's driving a car. Used by Vehicle.Kind and World.GetNearestVehicle.
| Value | Meaning |
|---|---|
Player (0) | Your car. |
Drivatar (1) | A Drivatar's car. |
Traffic (2) | A traffic car. |
Other (3) | Anything else. |
Indicators
Off, Left, Right, Hazards: for Vehicle.Indicators.
CarPart
What OpenPart, ClosePart and TogglePart move: Roof, Hood, Trunk, DoorFrontLeft, DoorFrontRight, DoorRearLeft, DoorRearRight, Storage, StorageMiddle, PopUpLights (both sides together), ActiveAero. A part only moves on a car that has it.
CarInfo
Vehicle.Model gives a CarInfo: the car's Id, ShortName ("Chevy Camaro SS") and FullName ("Camaro SS Coupe"). See Garage, CarCatalog and Profile.
Notes
- A
Vehicleis only valid during the frame you got it in. Cars come and go as the world streams, so don't keep one between ticks. Get it again each time. - Coordinates are the game's, in metres: X and Z across the map, Y up.
- Speeds are in m/s.
SpeedKmhandSpeedMphconvert for you. - Setting
Velocityanywhere but insideGame.PhysicsStep, for the car it gives you, throws anInvalidOperationException. The physics owns it the rest of the time. IsOnGroundcomes from the car's physics steps. It's true until the car has had a few.- Using a
Vehicleoff the game thread (and outsideGame.PhysicsStep) throws anInvalidOperationExceptionthat says to useGame.TickorGame.Run. - Exhaust flames go out when your mod is reloaded.
- On a game build the SDK doesn't know,
ExhaustFlamesandBackfiredo nothing, andplatform.logsays so once ("Vehicle.ExhaustFlames off").
