Adds a traffic vehicle to the nearest waypoint from the provided position. The standard Traffic System conditions, such as avoiding adding a vehicle if it is within the player's view, will be bypassed. It is the developer's responsibility to ensure that it is safe to add the vehicle.

This method will wait until a vehicle of that type is available, and the closest waypoint is free to add a new vehicle. The process runs in the background until the new vehicle is successfully added.


public static void AddVehicle(Vector3 position, VehicleTypes vehicleType)

public static void AddVehicle(Vector3 position, VehicleTypes vehicleType, UnityAction<VehicleComponent, int> completeMethod)




The position where to add a new vehicle. The closest waypoint to that position will be used.


The type of vehicle to add.


Callback triggered after instantiation. It returns the VehicleComponent and the waypoint index where the vehicle was instantiated.


using Gley.TrafficSystem;
using UnityEngine;

public class Test : MonoBehaviour
    //a reference to vehicle pool and player assigned in inspector
    public VehiclePool vehiclePool;
    public Transform player;

    void Start()
        API.Initialize(player, 20, vehiclePool);

    //hand-picked positions
    Vector3 startPosition = new Vector3(0, 0, 0);
    public void AddVehicle()
        //0 - is an example index of the vehicle
        API.AddVehicle(startPosition, VehicleTypes.Car, CompleteMethod);

    private void CompleteMethod(VehicleComponent vehicleScript, int waypointIndex)
        Debug.Log($"{} was instantiated on waypoint {waypointIndex}");

Last updated