OVRGrabbable.cs 4.7 KB

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