SetDestination

Description

Calculates a path from the current position of the vehicle to a specified destination. The destination will be the closest waypoint to the destination point.

Declaration

public static void SetDestination(int vehicleIndex, Vector3 position)

Parameters

NameDescription

vehicleIndex

The index of the vehicle.

position

The destination position.

Example

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);
        Events.onDestinationReached += DestinationReached;
    }

    public void SetDestination()
    {
        //set a random destination;
        Vector3 destination = new Vector3(Random.Range(-500, 500), 0, Random.Range(-500, 500));

        //make the vehicle with index 5 drive to the destination
        API.SetDestination(5, destination);
    }

    private void DestinationReached(int vehicleIndex)
    {
        if(vehicleIndex==5)
        {
            //set another random destination for this vehicle if the previous one was reached.
            Vector3 destination = new Vector3(Random.Range(-500, 500), 0, Random.Range(-500, 500));
            API.SetDestination(vehicleIndex, destination);
        }
    }

    private void OnDestroy()
    {
        Events.onDestinationReached -= DestinationReached;
    }
}

Last updated