# 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.

![](/files/CMF0ePqTiyF18SSKfx3k)

### 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xreal.gitbook.io/nrsdk/development/plane-detection-tutorial/add-a-car.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
