OVRGrabbable.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /************************************************************************************
  2. Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
  3. Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus VR Rift SDK except in compliance with the License,
  5. which is provided at the time of installation or download, or which
  6. otherwise accompanies this software in either electronic or hard copy form.
  7. You may obtain a copy of the License at
  8. https://developer.oculus.com/licenses/sdk-3.4.1
  9. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ************************************************************************************/
  15. using System;
  16. using UnityEngine;
  17. /// <summary>
  18. /// An object that can be grabbed and thrown by OVRGrabber.
  19. /// </summary>
  20. public class OVRGrabbable : MonoBehaviour
  21. {
  22. [SerializeField]
  23. protected bool m_allowOffhandGrab = true;
  24. [SerializeField]
  25. protected bool m_snapPosition = false;
  26. [SerializeField]
  27. protected bool m_snapOrientation = false;
  28. [SerializeField]
  29. protected Transform m_snapOffset;
  30. [SerializeField]
  31. protected Collider[] m_grabPoints = null;
  32. protected bool m_grabbedKinematic = false;
  33. protected Collider m_grabbedCollider = null;
  34. protected OVRGrabber m_grabbedBy = null;
  35. /// <summary>
  36. /// If true, the object can currently be grabbed.
  37. /// </summary>
  38. public bool allowOffhandGrab
  39. {
  40. get { return m_allowOffhandGrab; }
  41. }
  42. /// <summary>
  43. /// If true, the object is currently grabbed.
  44. /// </summary>
  45. public bool isGrabbed
  46. {
  47. get { return m_grabbedBy != null; }
  48. }
  49. /// <summary>
  50. /// If true, the object's position will snap to match snapOffset when grabbed.
  51. /// </summary>
  52. public bool snapPosition
  53. {
  54. get { return m_snapPosition; }
  55. }
  56. /// <summary>
  57. /// If true, the object's orientation will snap to match snapOffset when grabbed.
  58. /// </summary>
  59. public bool snapOrientation
  60. {
  61. get { return m_snapOrientation; }
  62. }
  63. /// <summary>
  64. /// An offset relative to the OVRGrabber where this object can snap when grabbed.
  65. /// </summary>
  66. public Transform snapOffset
  67. {
  68. get { return m_snapOffset; }
  69. }
  70. /// <summary>
  71. /// Returns the OVRGrabber currently grabbing this object.
  72. /// </summary>
  73. public OVRGrabber grabbedBy
  74. {
  75. get { return m_grabbedBy; }
  76. }
  77. /// <summary>
  78. /// The transform at which this object was grabbed.
  79. /// </summary>
  80. public Transform grabbedTransform
  81. {
  82. get { return m_grabbedCollider.transform; }
  83. }
  84. /// <summary>
  85. /// The Rigidbody of the collider that was used to grab this object.
  86. /// </summary>
  87. public Rigidbody grabbedRigidbody
  88. {
  89. get { return m_grabbedCollider.attachedRigidbody; }
  90. }
  91. /// <summary>
  92. /// The contact point(s) where the object was grabbed.
  93. /// </summary>
  94. public Collider[] grabPoints
  95. {
  96. get { return m_grabPoints; }
  97. }
  98. /// <summary>
  99. /// Notifies the object that it has been grabbed.
  100. /// </summary>
  101. virtual public void GrabBegin(OVRGrabber hand, Collider grabPoint)
  102. {
  103. m_grabbedBy = hand;
  104. m_grabbedCollider = grabPoint;
  105. gameObject.GetComponent<Rigidbody>().isKinematic = true;
  106. }
  107. /// <summary>
  108. /// Notifies the object that it has been released.
  109. /// </summary>
  110. virtual public void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
  111. {
  112. Rigidbody rb = gameObject.GetComponent<Rigidbody>();
  113. rb.isKinematic = m_grabbedKinematic;
  114. rb.velocity = linearVelocity;
  115. rb.angularVelocity = angularVelocity;
  116. m_grabbedBy = null;
  117. m_grabbedCollider = null;
  118. }
  119. void Awake()
  120. {
  121. if (m_grabPoints.Length == 0)
  122. {
  123. // Get the collider from the grabbable
  124. Collider collider = this.GetComponent<Collider>();
  125. if (collider == null)
  126. {
  127. throw new ArgumentException("Grabbables cannot have zero grab points and no collider -- please add a grab point or collider.");
  128. }
  129. // Create a default grab point
  130. m_grabPoints = new Collider[1] { collider };
  131. }
  132. }
  133. protected virtual void Start()
  134. {
  135. m_grabbedKinematic = GetComponent<Rigidbody>().isKinematic;
  136. }
  137. void OnDestroy()
  138. {
  139. if (m_grabbedBy != null)
  140. {
  141. // Notify the hand to release destroyed grabbables
  142. m_grabbedBy.ForceRelease(this);
  143. }
  144. }
  145. }