GetPath

Description

Returns a waypoint path between a start position and an end position for a specific vehicle type. The result will be a list of waypoint indexes.

Declaration

public static List<int> GetPath(Vector3 startPosition, Vector3 endPosition, VehicleTypes vehicleType)

Parameters

NameDescription

startPosition

A Vector3 for the initial position. The path will be calculated from the closest waypoint located near the startPosition.

endPosition

A Vector3 for the final position. The path will be calculated to the closest waypoint located near the endPosition.

vehicleType

The vehicle type for which this path is intended.

Returns

List<int> - the waypoint indexes of the path between startPosition and endPosition.

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