Vehicle Pool

Each scene requires a Vehicle Pool to work.

A vehicle pool is a collection of all vehicles available on the current scene.

Create a Vehicle Pool

Right-click on the Project Window -> Create -> Traffic System -> Vehicle Pool

This will create a scriptable object inside the project folder.

Add vehicles to the pool

There is no limit for the pool vehicles.

Each entry represents a type of vehicle that will be available on the roads. Each vehicle needs to be configured according to the Vehicle Implementation

If a vehicle type is added to the pool, it ensures that at least one vehicle of that type will be available on the roads, unless the maximum number of vehicles is less than the total number of vehicle types. For example, in the scenario mentioned above, the maximum number of vehicles needs to be equal to or greater than 5.

The percentage value represents the proportional allocation of each vehicle type out of the total instantiated vehicles, functioning more like a comparison slider than a true percentage. In the given example, assuming a maximum availability of 20 vehicles, the distribution of vehicles based on the given percentages results in a total 'percentage' value of 10+20+20+5+1 = 56.

It's important to note that 1 vehicle of each type will be instantiated regardless of the percentage specified. Therefore, recalculating the new percentage considering the already instantiated vehicles results in 9+19+19+4+0 = 51, leaving 15 additional cars to be instantiated.

This equates to approximately 51/15=3.4 'percent' per vehicle type, resulting in the instantiation of 9/3.4=2 Truck+Trailer, 5 Car, 5 Sedan, 1 SemiTruck, and an additional 2 vehicles, which might be Car and Sedan, depending on the rounding of the percentage.

The Don't Instantiate checkbox prevents the specified type of vehicles from being used by the system. These vehicles will be available but remain disabled by default until specifically activated by the developer. This option proves useful when you want certain types of vehicles available in specific areas of your map or cities. For instance, you might have a construction site where construction vehicles should only appear when the player is near that area.

Activating these vehicles requires coding skills as they can only be enabled using the API. Below is an example of how to use the API:

GameObject instantiateLaterVehicle;

//after the traffic system is initialized
void OnTrafficInitializedCallback()
    //get the complete list of excluded vehicle
    List<VehicleComponent> excludedVehicle = API.GetExcludedVehicleList();

    foreach (VehicleComponent vehicleComponent in excludedVehicle)
        if (vehicleComponent.vehicleType == VehicleTypes.SomeType)
            //do something
            //save the vehicle
            //do whatever you want

            //I will save the GamaObject for later use
            instantiateLaterVehicle = vehicleComponent.gameObject;

//at the right time instantiate the vehicle using one of those 2 options
void InstantiateExcludedVehicle()
    //manually add vehicle to a specified position
    API.AddExcludedVehicle(API.GetExcludedVehicleIndex(instantiateLaterVehicle), position);

    //add the vehicle to the system and it will be automatically instantiated like any other vehicle

Last updated