HelloMRController.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. using UnityEngine;
  10. namespace NRKernal.NRExamples
  11. {
  12. /// <summary> Controls the HelloAR example. </summary>
  13. [HelpURL("https://developer.nreal.ai/develop/unity/controller")]
  14. public class HelloMRController : MonoBehaviour
  15. {
  16. /// <summary> A model to place when a raycast from a user touch hits a plane. </summary>
  17. public GameObject AndyPlanePrefab;
  18. /// <summary> Updates this object. </summary>
  19. void Update()
  20. {
  21. // If the player doesn't click the trigger button, we are done with this update.
  22. if (!NRInput.GetButtonDown(ControllerButton.TRIGGER))
  23. {
  24. return;
  25. }
  26. // Get controller laser origin.
  27. var handControllerAnchor = NRInput.DomainHand == ControllerHandEnum.Left ? ControllerAnchorEnum.LeftLaserAnchor : ControllerAnchorEnum.RightLaserAnchor;
  28. Transform laserAnchor = NRInput.AnchorsHelper.GetAnchor(NRInput.RaycastMode == RaycastModeEnum.Gaze ? ControllerAnchorEnum.GazePoseTrackerAnchor : handControllerAnchor);
  29. RaycastHit hitResult;
  30. if (Physics.Raycast(new Ray(laserAnchor.transform.position, laserAnchor.transform.forward), out hitResult, 10))
  31. {
  32. if (hitResult.collider.gameObject != null && hitResult.collider.gameObject.GetComponent<NRTrackableBehaviour>() != null)
  33. {
  34. var behaviour = hitResult.collider.gameObject.GetComponent<NRTrackableBehaviour>();
  35. if (behaviour.Trackable.GetTrackableType() != TrackableType.TRACKABLE_PLANE)
  36. {
  37. return;
  38. }
  39. // Instantiate Andy model at the hit point / compensate for the hit point rotation.
  40. Instantiate(AndyPlanePrefab, hitResult.point, Quaternion.identity, behaviour.transform);
  41. }
  42. }
  43. }
  44. }
  45. }