# Add a car

The player will control a toy car that will drive towards the location of the reticle. A model for this car is provided in the Plane Detection Starter Package.

![](https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FDNCXzZOE8SKW0voLI4Hb%2Fimage.png?alt=media\&token=75b27d49-9589-4f17-9d9f-397a36aa4fd0)

### Define Car Behaviour

1. Create a `CarBehaviour` script in `Assets/PlaneDetectionStarterPackage`. The script defines how a car moves in relation to the hit point (reticle).

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarBehaviour : MonoBehaviour
{
    public ReticleBehaviour Reticle;
    public float Speed = 1.2f;

    private void Update()
    {
        var trackingPosition = Reticle.transform.position;
        if (Vector3.Distance(trackingPosition, transform.position) < 0.2)
        {
            return;
        }

        var lookRotation = Quaternion.LookRotation(trackingPosition - transform.position);
        transform.rotation =
            Quaternion.Lerp(transform.rotation, lookRotation, Time.deltaTime * 10f);
        transform.position =
            Vector3.MoveTowards(transform.position, trackingPosition, Speed * Time.deltaTime);
    }

}

```

2\. Double click `Car Prefab` in `Assets/PlaneDetectionStarterPackage` to edit the prefab. Add the `CarBehaviour` you've created as a component of this prefab.&#x20;

### Create `CarManager` script

Create a `CarManager` in your project's scripts folder (`Assets/Scripts`) as the following. The `CarManager` defines when and how the car is initialized.

```csharp
using System.Collections;
using System.Collections.Generic;
using NRKernal;
using UnityEngine;

public class CarManager : MonoBehaviour
{
    public GameObject CarPrefab;
    public ReticleBehaviour Reticle;
    public CarBehaviour Car;

    private GameObject LockedPlane;

    private void Update()
    {
        if (Car == null && WasTapped() && Reticle.CurrentPlane != null)
        {
            // Spawn our car at the reticle location.
            var obj = Instantiate(CarPrefab);
            Car = obj.GetComponent<CarBehaviour>();
            Car.Reticle = Reticle;
            Car.transform.position = Reticle.transform.position;
        }
    }

    private bool WasTapped()
    {
        return NRInput.GetButtonDown(ControllerButton.TRIGGER);
    }
}// Some code
```

### Add a `CarManager` to your scene <a href="#add-a-carmanager-to-your-scene" id="add-a-carmanager-to-your-scene"></a>

1. In the **Hierarchy**, create a new empty `GameObject`.
2. Rename it to `Car Spawner`.
3. Select the object you created. In the **Hierarchy** pane, click **Add Component** to add the `CarManager` component.
4. Set up `CarManager`'s dependencies by clicking on the chooser for each field:
   * **Car Prefab**: In **Assets**, select **Car Prefab**.
   * **Reticle**: In **Scene**, select **Reticle Prefab**.

### Test driving <a href="#test-driving" id="test-driving"></a>

1. Click **File > Build** to test your changes.
2. When you tap on a plane, you should see a small car appear at that location. This car will follow the reticle.
