NRGrabbableObject.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. using System;
  13. /// <summary> A nr grabbable object. </summary>
  14. [RequireComponent(typeof(Rigidbody))]
  15. public class NRGrabbableObject : MonoBehaviour {
  16. /// <summary> Gets a value indicating whether we can grab. </summary>
  17. /// <value> True if we can grab, false if not. </value>
  18. public bool CanGrab { get { return Grabber == null; } }
  19. /// <summary> Gets a value indicating whether this object is being grabbed. </summary>
  20. /// <value> True if this object is being grabbed, false if not. </value>
  21. public bool IsBeingGrabbed { get { return Grabber; } }
  22. /// <summary> Gets or sets the grabber. </summary>
  23. /// <value> The grabber. </value>
  24. public NRGrabber Grabber { get; private set; }
  25. /// <summary> Gets the attached colliders. </summary>
  26. /// <value> The attached colliders. </value>
  27. public Collider[] AttachedColliders
  28. {
  29. get
  30. {
  31. CheckAttachedColliders();
  32. return m_AttachedColliders;
  33. }
  34. }
  35. /// <summary> Event queue for all listeners interested in OnGrabBegan events. </summary>
  36. public event Action OnGrabBegan { add { m_OnGrabBegan += value; } remove { m_OnGrabBegan -= value; } }
  37. /// <summary> Event queue for all listeners interested in OnGrabEnded events. </summary>
  38. public event Action OnGrabEnded { add { m_OnGrabEnded += value; } remove { m_OnGrabEnded -= value; } }
  39. /// <summary> The attached rigidbody. </summary>
  40. protected Rigidbody m_AttachedRigidbody;
  41. /// <summary> The attached colliders. </summary>
  42. [SerializeField]
  43. private Collider[] m_AttachedColliders;
  44. /// <summary> True to origin rigidbody kinematic. </summary>
  45. private bool m_OriginRigidbodyKinematic;
  46. /// <summary> The on grab began. </summary>
  47. private Action m_OnGrabBegan;
  48. /// <summary> The on grab ended. </summary>
  49. private Action m_OnGrabEnded;
  50. /// <summary> Awakes this object. </summary>
  51. protected virtual void Awake()
  52. {
  53. m_AttachedRigidbody = GetComponent<Rigidbody>();
  54. m_OriginRigidbodyKinematic = m_AttachedRigidbody.isKinematic;
  55. CheckAttachedColliders();
  56. }
  57. /// <summary> Grab begin. </summary>
  58. /// <param name="grabber"> The grabber.</param>
  59. public void GrabBegin(NRGrabber grabber)
  60. {
  61. if (IsBeingGrabbed || grabber == null)
  62. return;
  63. gameObject.GetComponent<Rigidbody>().isKinematic = true;
  64. Grabber = grabber;
  65. if (m_OnGrabBegan != null)
  66. {
  67. m_OnGrabBegan();
  68. }
  69. }
  70. /// <summary> Grab end. </summary>
  71. public void GrabEnd()
  72. {
  73. m_AttachedRigidbody.isKinematic = m_OriginRigidbodyKinematic;
  74. Grabber = null;
  75. if (m_OnGrabEnded != null)
  76. {
  77. m_OnGrabEnded();
  78. }
  79. }
  80. /// <summary> Move rigidbody. </summary>
  81. /// <param name="targetPos"> Target position.</param>
  82. /// <param name="targetRot"> Target rot.</param>
  83. public void MoveRigidbody(Vector3 targetPos, Quaternion targetRot)
  84. {
  85. if (!IsBeingGrabbed)
  86. return;
  87. m_AttachedRigidbody.MovePosition(targetPos);
  88. m_AttachedRigidbody.MoveRotation(targetRot);
  89. }
  90. /// <summary> Move transform. </summary>
  91. /// <param name="targetPos"> Target position.</param>
  92. /// <param name="targetRot"> Target rot.</param>
  93. public void MoveTransform(Vector3 targetPos, Quaternion targetRot)
  94. {
  95. if (!IsBeingGrabbed)
  96. return;
  97. transform.position = targetPos;
  98. transform.rotation = targetRot;
  99. }
  100. /// <summary> Check attached colliders. </summary>
  101. private void CheckAttachedColliders()
  102. {
  103. if (m_AttachedColliders != null && m_AttachedColliders.Length > 0)
  104. return;
  105. m_AttachedColliders = GetComponentsInChildren<Collider>();
  106. if (m_AttachedColliders == null)
  107. {
  108. NRDebugger.Error("AttachedColliders can not be null for NRGrabbableObject, please set collider !");
  109. }
  110. }
  111. }
  112. }