# Tutorial: Mesh Collision

### What you'll build <a href="#what-youll-build" id="what-youll-build"></a>

In this tutorial, we will enable meshing and demonstrate how to collide objects with the generated mesh.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FsnWbXI4KWSYBW9CxreSA%2FshootBalls.gif?alt=media&#x26;token=8c4e9202-759a-469f-8420-aa90bd579b4d" alt=""><figcaption></figcaption></figure>

### What you'll need <a href="#what-youll-need" id="what-youll-need"></a>

This tutorial assumes you have a working Unity scene with the NRSDK imported. Please refer to the [Getting Started with NRSDK](../../nrsdk-fundamentals/quickstart-for-android) page for more details on how to import the NRSDK package.

## 1. Create a new scene

create a new folder named `MeshCollsion`, and another folder named `Scene`, and then create a new scene named `MeshCollsion`<mark style="color:purple;">.</mark>

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FElc9HK4JyKkcfsOxCOqR%2Fimage.png?alt=media&#x26;token=35f6dd7a-7eee-4bef-9afc-a88fec6ec2b2" alt=""><figcaption></figcaption></figure>

Add the `NRCameraRig` and `NRInput` prefab into the hierarchy.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FRF0nA9uyrvkA6RKjBsrx%2Fimage.png?alt=media&#x26;token=1a39dc80-8015-4b52-8648-1748ff3c681f" alt=""><figcaption></figcaption></figure>

## 2. Add the MeshingManager

Create an empty GameObject called MeshingManager.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FRxP6vpsEA8CNPg1KZEMA%2Fimage.png?alt=media&#x26;token=04647a2e-6523-4a27-b996-6f6aecfc93f2" alt=""><figcaption></figcaption></figure>

Add the following scripts to Meshing Manager:

1. NRMeshingManager.cs
2. MeshObjGenerator.cs

{% hint style="info" %}
If you want to save the mesh for tests, you can add the `MeshSaver.cs`too.
{% endhint %}

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2F6fEANdXmEomEEUUpTN1J%2Fimage.png?alt=media&#x26;token=7d251b71-b350-4410-913a-5cb2473d5d69" alt=""><figcaption></figcaption></figure>

## 3. Add a sphere

Right-click on the scene hierarchy and click **Game Object**> **3D Object**> **Sphere**.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FFhUPS5VwITXtSL9Q31p0%2Fimage.png?alt=media&#x26;token=17b48086-2b7d-4d04-8581-2504eb42f2f2" alt=""><figcaption></figcaption></figure>

Click on the sphere and set its size to 0.05.&#x20;

Then add a rigid body component to the sphere. This tells Unity to give the sphere physics properties so it will fall down unless it hits something. You can also apply forces to it to move it around, simulate throwing it, etc. Note that if you don't change the `Collision Detection` to **Continuous Dynamic,** sometimes your ball will pass directly through the mesh, with no collision effect.

Add a material to the sphere to make it more obvious.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2F5LlTUEwEvHQH0ZTnvpJ1%2Fimage.png?alt=media&#x26;token=02f816e0-eeec-4f9f-95b4-29cdca2f9715" alt=""><figcaption></figcaption></figure>

Create a folder called Prefabs, and drag the sphere to the folder.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2FihCP4iPLWVbhJp8nllvw%2Fimage.png?alt=media&#x26;token=77907861-bb2e-4bde-af61-04f6e047ca6c" alt=""><figcaption></figcaption></figure>

## 4. Add code to shoot the sphere

Each time the user taps the touchpad, a ball will be shooted in place of the ray cast from the phone's controller.

By attaching the following script to **NRCameraRig**, you can customize the shoot position of the sphere. This tutorial uses the ray cast position, `Model Anchor`, as the emission point.

Set the prefab Sphere you just made as the `Ball Prefab`.

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2F2nNzBrdLJVbWpPLvPhIt%2Fimage.png?alt=media&#x26;token=ff868ce1-de32-4e14-9c02-e79fea8717b9" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1363773068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FL6MonFe6Phoj4t5Xi1d0%2Fuploads%2F5c77AxISYa8WJSPo73HD%2Fimage.png?alt=media&#x26;token=0c828437-5d96-4f92-8285-7acc97658656" alt=""><figcaption></figcaption></figure>

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

public class Shoot : MonoBehaviour
{
    public float moveSpeed = 10f;
    public GameObject shootPos;
    public float force = 1000;
    public GameObject ballPrefab;

    Vector3 targetObjectNextPosition;

    RaycastHit hit;

    void FixedUpdate()
    {
        if (NRInput.GetButtonDown(ControllerButton.TRIGGER))
        {
            var handControllerAnchor =
                NRInput.DomainHand == ControllerHandEnum.Left
                    ? ControllerAnchorEnum.LeftLaserAnchor
                    : ControllerAnchorEnum.RightLaserAnchor;
            Transform laserAnchor = NRInput.AnchorsHelper.GetAnchor(
                NRInput.RaycastMode == RaycastModeEnum.Gaze
                    ? ControllerAnchorEnum.GazePoseTrackerAnchor
                    : handControllerAnchor
            );

            Ray ray =new Ray(laserAnchor.transform.position, laserAnchor.transform.forward);

            Rigidbody ball = Instantiate(ballPrefab, shootPos.transform.position, Quaternion.identity).GetComponent<Rigidbody>() as Rigidbody;
            ball.AddForce(force * ray.direction);
            
        }
    }

}
 
```

## 5. Destroy the sphere after a period of time

To save scene resources, use the invoke function to destroy the sphere after a specified time.

Attach the script to the sphere prefab made in Step 3.

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

public class sphere : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Invoke("DelayDestroy", 10); // * destroy the prefab after 10 seconds
    }

    // Update is called once per frame
    void DelayDestroy()
    {
        Destroy(this.gameObject);
    }
}

```

## 6. Build and Run
