Instantiates a vehicle at the nearest waypoint to the provided position and sets the vehicle's path to the closest waypoint to the provided destination.

The instantiation occurs only when a vehicle of the specified type is available (disabled and ready for instantiation). The method will wait until a vehicle becomes available.

It is the developer's responsibility to choose a position out of view, as no additional verification will be made, and the vehicle might appear in front of the player.


public static void AddVehicleWithPath(Vector3 position, VehicleTypes vehicleType, Vector3 destination)

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




A Vector3 for the initial position. The vehicle will be placed on the closest waypoint from this position.


The type of the vehicle to be instantiated.


A Vector3 for the destination position. The closest waypoint from this position will be the destination of the vehicle.


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);
    Vector3 endPosition = new Vector3(0, 0, 350);

    public void AddVehicle()
        API.AddVehicleWithPath(startPosition, VehicleTypes.Car, endPosition, CompleteMethod);

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

Last updated