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