🕶️
NRSDK(Old)
New DocumentationSDK DownloadAPI Reference
v2.2.1
v2.2.1
  • NRSDK Fundamentals
    • NRSDK Overview
    • XREAL Devices
      • XREAL Glasses
      • Controller
      • Compatibility
    • Getting Started with NRSDK
    • Sample Code
    • Tutorials
    • Release Note
      • NRSDK 2.2.1
      • NRSDK 2.2.0
      • NRSDK 2.1.1
      • NRSDK 2.1.0
      • NRSDK 1.10.2
      • NRSDK 1.9.5
      • NRSDK 1.9.3
      • NRSDK 1.9.1
      • NRSDK 1.8.0
      • NRSDK 1.7.0
      • NRSDK 1.6.0
  • Development
    • Input and Camera
      • NRInput
      • Interact with Unity UI (Tutorial)
      • Customize Controller UI
      • NRCameraRig
    • Hand Tracking
    • Image Tracking
      • XREAL Markers
    • Plane Detection (Tutorial)
      • Overview
      • Import the package
      • Detect planes in the real world
      • Perform a hit test against detected planes
      • Add a car
      • Add gems
      • Wrap up
    • Depth Mesh
      • Meshing Manager Overview
      • Use Meshes in the Editor
      • Tutorial: Mesh Collision
    • Spatial Anchor
      • Mapping Example Scene
      • Tutorial: Halloween Treasure Hunt
        • Handle the Situation of Failed Anchor Saving
      • Tutorial: Sharing Anchors
        • Setting Up Photon
        • Cloud Storage: Firebase (optional)
        • Cloud Storage: Aliyun OSS (optional)
        • Implementing Cloud Save and Load
        • Sharing Anchors with Photon
    • Tools
      • Single Pass Stereo Rendering
      • First Person View
      • Emulator
      • XR Streaming
      • Dual Screen Display
    • Miscellaneous
      • Access RGB Camera
      • NRSDK Coordinate Systems
      • MRTK2 Integration
      • MRTK3 Integration
      • Notification popup
      • Reset Camera
      • Render Metrics
      • Render MonoMode(Obsolete)
  • API Reference
  • Frequently Asked Questions
  • Design Guide
    • Design Guide Overview
    • Displaying
    • Interacting
    • Controlling
    • Navigating
Powered by GitBook
On this page
  • Create GemSpawner
  • Add the game element
  • Create GemBehaviour
  • Modify Car Behaviour
  • Test the game

Was this helpful?

  1. Development
  2. Plane Detection (Tutorial)

Add gems

PreviousAdd a carNextWrap up

Was this helpful?

Now that the player can control an entity in the scene, give the player a destination to drive towards.

Create GemSpawner

  1. In Assets/Scripts, create a new script named GemSpawner. This script defines how the gems are generated randomly on the current plane.

using NRKernal;
using NRKernal.NRExamples;
using UnityEngine;

public class GemSpawner : MonoBehaviour
{
    public GemBehaviour Gem;
    public GameObject GemPrefab;
    public ReticleBehaviour Reticle;

    public static Vector3 RandomInTriangle(Vector3 v1, Vector3 v2)
    {
        float u = Random.Range(0.0f, 1.0f);
        float v = Random.Range(0.0f, 1.0f);
        if (v + u > 1)
        {
            v = 1 - v;
            u = 1 - u;
        }

        return (v1 * u) + (v2 * v);
    }

    public Vector3 FindRandomLocation(GameObject plane)
    {
        // Select random triangle in Mesh
        var mesh = plane.GetComponent<PolygonPlaneVisualizer>().m_PlaneMesh;
        var triangles = mesh.triangles;
        var triangle = triangles[(int) Random.Range(0, triangles.Length - 1)] / 3 * 3;
        var vertices = mesh.vertices;
        var randomInTriangle = RandomInTriangle(vertices[triangle], vertices[triangle + 1]);
        var randomPoint = plane.transform.TransformPoint(randomInTriangle);
        randomPoint.y = Reticle.CurrentPlane.GetComponent<NRTrackableBehaviour>().Trackable.GetCenterPose().position.y;
        return randomPoint;
    }

    public void SpawnGem(GameObject plane)
    {
        var gemClone = Instantiate(GemPrefab);
        gemClone.transform.position = FindRandomLocation(plane);

        Gem = gemClone.GetComponent<GemBehaviour>();
    }

    private void Update()
    {
        if (Reticle.CurrentPlane != null)
        {
            if (Gem == null)
            {
                SpawnGem(Reticle.CurrentPlane);
            }
        }
    }
}

Add the game element

  1. Create a new empty GameObject in the Hierarchy.

  2. Rename it to Gem Spawner.

  3. Select the object you created. In the Hierarchy pane, click Add Component to add the GemSpawner component to it.

  4. Set up GemSpawner's dependencies by clicking on the chooser for the following field:

    • Gem Prefab: In Assets, select Gem Prefab.

    • Reticle: In Scene, select Reticle Prefab.

Create GemBehaviour

  1. Create a script named GemBehaviour in PlaneDetectionStarterPackage folder. You needn't add anything else to this script.

  2. Associate GemBehaviour with Gem Prefab by editing Gem Prefab, clicking Add Component, and choose GemBehaviour you've created.

Modify Car Behaviour

In CarBehaviour you've previously created, add the following event function which defines the action when the car hits the gem.

    private void OnTriggerEnter(Collider other)
    {
        var Gem = other.GetComponent<GemBehaviour>();
        if (Gem != null)
        {
            Destroy(other.gameObject);
        }
    }

Test the game

  1. Click File > Build to test your changes. 2, After you create a car, a gem should spawn.

  2. Drive your car to the gem.

  3. A new one will appear at a random location.