A Vehicle component represents an object that the player can control to move around in the game world.

Creating A Vehicle

To begin creating a new vehicle of any kind, create a new game object in the scene and add a Vehicle component.

Destroying/Restoring Vehicles

Like Game Agents, vehicles can be destroyed or restored during gameplay. Here is some code to help you understand how you can do this:

// Destroy a vehicle

// Restore a vehicle

// Check if the vehicle is destroyed
if (myVehicle.Destroyed)
    // Some code

Vehicle Occupant

Sometimes you might want to know what game agent is controlling a vehicle.

// Get vehicle occupants
List<GameAgents> occupants = myVehicle.Occupants;

Note that the Vehicle component has been scripted to allow for multiple occupants, but currently the kits only support one occupant per vehicle. So the list will normally have one member when a vehicle is occupied, and be empty when it is not.

Sometimes you might want to do something different when a player enters the vehicle vs when an AI enters the vehicle, or when the occupant exits a vehicle. You will find Unity Events in the inspector you can add functions to that you wish to be called when any of these events occur.

Module Mounts

To get all the module mounts on a vehicle, or add/remove a module mount, use the following code:

// Get a list of all the module mounts on the vehicle
List<ModuleMount> moduleMounts = vehicle.ModuleMounts;

// Add a module mount to the vehicle

// Remove a module mount from the vehicle

Vehicle Classes

The Vehicle Class (which is a Scriptable Object) provides a way to differentiate between vehicles, for example for running specific input scripts or camera controllers.

To create a new Vehicle Class, right click in the Project view and select Create > VSX > Vehicle Class.

This will create a new Vehicle Class object in your Assets folder, which you can rename.

To assign a Vehicle Class to a Vehicle, simply drag the Vehicle Class object you created into the 'Vehicle Class' field in the inspector of the Vehicle component.

Last updated