//on exiting collision with another collider, remove that collider from candidates if present void OnTriggerExit(Collider otherCollider) { CustomGrabbable grabbable = otherCollider.GetComponent <CustomGrabbable>() ?? otherCollider.GetComponentInParent <CustomGrabbable>(); if (grabbable == null) { return; } // Remove the grabbable int refCount = 0; bool found = m_grabCandidates.TryGetValue(grabbable, out refCount); if (!found) { return; } if (refCount > 1) { m_grabCandidates[grabbable] = refCount - 1; } else { m_grabCandidates.Remove(grabbable); } }
private void GrabEnd() { OVRPose localPose = new OVRPose { position = OVRInput.GetLocalControllerPosition(m_OVRController), orientation = OVRInput.GetLocalControllerRotation(m_OVRController) }; OVRPose trackingSpace = transform.ToOVRPose() * localPose.Inverse(); Vector3 linearVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerVelocity(m_OVRController); Vector3 angularVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerAngularVelocity(m_OVRController); angularVelocity *= -1; m_grabbedObject.GrabEnd(this, linearVelocity, angularVelocity); // We also shoot out an event that tells our event system that something was let go switch (m_OVRController) { case (OVRInput.Controller.LTouch): CustomEvents.current.LeftGrabbedTurnedOff(m_grabbedObject); break; case (OVRInput.Controller.RTouch): CustomEvents.current.RightGrabbedTurnedOff(m_grabbedObject); break; } CustomEvents.current.GrabbedTurnedOff(this, OVRController, m_grabbedObject); m_grabbedObject = null; }
//stops pinching the currently pinched object protected void PinchEnd() { m_isZooming = false; m_isPinching = false; m_pinchedObj.pinches--; m_pinchedObj.gameObject.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.None; m_pinchedObj = null; GrabVolumeEnable(true); }
public void ForceRelease(CustomGrabbable grabbable) { bool canRelease = ( (m_grabbedObj != null) && (m_grabbedObj == grabbable) ); if (canRelease) { PinchEnd(); } }
private void GrabEnd() { OVRPose localPose = new OVRPose { position = OVRInput.GetLocalControllerPosition(m_OVRController), orientation = OVRInput.GetLocalControllerRotation(m_OVRController) }; OVRPose trackingSpace = transform.ToOVRPose() * localPose.Inverse(); Vector3 linearVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerVelocity(m_OVRController); Vector3 angularVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerAngularVelocity(m_OVRController); angularVelocity *= -1; m_grabbedObject.GrabEnd(this, linearVelocity, angularVelocity); m_grabbedObject = null; }
//retrieves the correct candidate for pinch and starts pinching said object, if that object already has another hand pinching it protected virtual void PinchBegin() { float closestMagSq = float.MaxValue; CustomGrabbable closestGrabbable = null; Collider closestGrabbableCollider = null; // Iterate grab candidates and find the closest grabbable candidate foreach (CustomGrabbable grabbable in m_grabCandidates.Keys) { bool canPinch = !(grabbable.pinches == 2); if (!canPinch) { continue; } for (int j = 0; j < grabbable.grabPoints.Length; ++j) { Collider grabbableCollider = grabbable.grabPoints[j]; // Store the closest grabbable Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(m_gripTransform.position); float grabbableMagSq = (m_gripTransform.position - closestPointOnBounds).sqrMagnitude; if (grabbableMagSq < closestMagSq) { closestMagSq = grabbableMagSq; closestGrabbable = grabbable; closestGrabbableCollider = grabbableCollider; } } } // Disable grab volumes to prevent overlaps GrabVolumeEnable(false); if (closestGrabbable != null) { m_isPinching = true; closestGrabbable.pinches++; m_pinchedObj = closestGrabbable; if (m_pinchedObj.pinches == 2) { m_isZooming = true; m_pinchedObj.gameObject.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY; m_handsDistanceAtStart = Vector3.Distance(transform.position, m_secondHand.transform.position); m_grabbableScaleAtStart = m_pinchedObj.transform.localScale.x; } } }
//on entering collision with another collider, add that collider to candidates void OnTriggerEnter(Collider otherCollider) { // Get the grab trigger CustomGrabbable grabbable = otherCollider.GetComponent <CustomGrabbable>() ?? otherCollider.GetComponentInParent <CustomGrabbable>(); if (grabbable == null) { return; } // Add the grabbable int refCount = 0; m_grabCandidates.TryGetValue(grabbable, out refCount); m_grabCandidates[grabbable] = refCount + 1; }
//private void AddColorGrabber(GameObject _obj) { // _obj.AddComponent<ColorGrabber>(); //} private void MakeGrabbable(GameObject _obj) { Rigidbody _rb = _obj.AddComponent <Rigidbody>(); _rb.useGravity = false; _rb.isKinematic = true; CapsuleCollider _coll = _obj.AddComponent <CapsuleCollider>(); _coll.height = 9; _coll.radius = 1; CustomGrabbable _grabbable = _obj.AddComponent <CustomGrabbable>(); _grabbable.enabled = true; _grabbable.M_GrabPoints = _obj.GetComponents <CapsuleCollider>(); }
private void GrabBegin(CustomGrabbable c) { if (c.GrabBegin(this)) { m_grabbedObject = c; } // We also shoot out an event that tells our event system that something was let go switch (m_OVRController) { case (OVRInput.Controller.LTouch): CustomEvents.current.LeftGrabbedTurnedOn(c); break; case (OVRInput.Controller.RTouch): CustomEvents.current.RightGrabbedTurnedOn(c); break; } CustomEvents.current.GrabbedTurnedOn(this, OVRController, c); }
/* * private IEnumerator CheckGrip() { * while(true) { * //grabVol.GetComponent<Renderer>().enabled = (OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, m_controller) > 0.1f); * if (OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger, m_controller) > 0.1f) { * if (debugToggle) grabVol.GetComponent<Renderer>().enabled = true; * // If the grip is being held down * if (m_grabbedObject == null) { * // Check if any objects are in range * inRange = grabVol.GetInRange(); * // Find Closest * GameObject closest = GetClosestInRange(); * // If there is a closest, then we initialize the grab * if (closest != null) { * GrabBegin(closest.GetComponent<CustomGrabbable>()); * } * } * } else { * if (debugToggle) grabVol.GetComponent<Renderer>().enabled = false; * if (m_grabbedObject != null) { * GrabEnd(); * } * } * yield return null; * } * } * * private GameObject GetClosestInRange() { * GameObject closest = null; * float closestDistance = 0f; * float d = 0f; * foreach(GameObject cg in inRange) { * if (!cg.GetComponent<CustomGrabbable>().CanBeGrabbed()) { continue; } * d = Vector3.Distance(m_gripTrans.position, cg.transform.position); * if (closest == null || d < closestDistance) { * closest = cg; * closestDistance = d; * } * } * return closest; * } */ private void GrabBegin(CustomGrabbable c) { c.GrabBegin(this); m_grabbedObject = c; }
public void GrabbedTurnedOff(CustomGrabber grabber, OVRInput.Controller c, CustomGrabbable cg) { onGrabbedTurnedOff?.Invoke(grabber, c, cg); }
public void RightGrabbedTurnedOff(CustomGrabbable cg) { onRightGrabbedTurnedOff?.Invoke(cg); }
public void LeftGrabbedTurnedOff(CustomGrabbable cg) { onLeftGrabbedTurnedOff?.Invoke(cg); }
public void GrabbedChanged(CustomGrabber grabber, OVRInput.Controller c, CustomGrabbable cg) { onGrabbedChanged?.Invoke(grabber, c, cg); }
public void RightHandGrabbedChanged(OVRInput.Controller c, CustomGrabbable cg) { onRightHandGrabbedChanged?.Invoke(c, cg); }