> For the complete documentation index, see [llms.txt](https://gley.gitbook.io/mobile-traffic-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gley.gitbook.io/mobile-traffic-system/complete-api/setdestination.md).

# 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&#x20;

```csharp
public static void SetDestination(int vehicleIndex, Vector3 position)
```

### Parameters

<table><thead><tr><th width="188">Name</th><th>Description</th></tr></thead><tbody><tr><td><strong>vehicleIndex</strong></td><td>The index of the vehicle.</td></tr><tr><td><strong>position</strong></td><td>The destination position.</td></tr></tbody></table>

### Example

```csharp
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;
    }
}

```
