SetVehiclePath

Description

A specific predefined path can be assigned to any active vehicle within the Traffic System.

The path consists of a list of waypoint indexes, and the vehicle will follow these waypoints instead of requesting random ones.

If a vehicle has a predefined path, it will not be removed by the pooling system.

Declaration

public static void SetVehiclePath(int vehicleIndex, Queue<int> pathWaypoints)

Parameters

NameDescription

vehicleIndex

The index of the vehicle.

pathWaypoints

A Queue of path waypoints indexes.

Example

using Gley.TrafficSystem;
using System.Collections.Generic;
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);
        //listen for the destination reached event
        Events.onDestinationReached += OnDestinationReached;
    }

    public void AddPathToVehicle()
    {
        //get the path from start to end
        Vector3 startPosition = new Vector3(0, 0, 0);
        Vector3 endPosition = new Vector3(0, 0, 350);
        List<int> path = API.GetPath(startPosition, endPosition, VehicleTypes.Car);

        if (path.Count > 0)
        {
            //assign the path to the vehicle with index 10
            API.SetVehiclePath(10, new Queue<int>(path));
        }
        else
        {
            Debug.Log("No path found");
        }
    }

    private void OnDestinationReached(int vehicleIndex)
    {
        //when vehicle 10 reaches the destination, remove the path and continue as a normal vehicle.
        if(vehicleIndex == 10)
        {
            API.RemoveVehiclePath(10);
        }
    }

    private void OnDestroy()
    {
        //don't forget to remove the events
        Events.onDestinationReached -= OnDestinationReached;
    }
}

Last updated