/// <summary> /// Release everything snapped to us /// </summary> public void ReleaseAll() { // No need to keep checking if (HeldItem == null) { return; } // Still need to keep track of item if we can't fully drop it if (!CanDropItem && HeldItem != null) { trackedItem = HeldItem; } HeldItem.ResetScale(); if (DisableColliders && disabledColliders != null) { foreach (var c in disabledColliders) { if (c) { c.enabled = true; } } } disabledColliders = null; // Reset Kinematic status if (heldItemRigid) { heldItemRigid.isKinematic = heldItemWasKinematic; } HeldItem.enabled = true; HeldItem.transform.parent = null; // Play Unsnap sound if (HeldItem != null) { if (SoundOnSnap) { VRUtils.Instance.PlaySpatialClipAt(SoundOnUnsnap, transform.position, 0.75f); } // Call event if (OnDetachEvent != null) { OnDetachEvent.Invoke(HeldItem); } // Fire Off Grabbable Events GrabbableEvents[] ge = HeldItem.GetComponents <GrabbableEvents>(); if (ge != null) { for (int x = 0; x < ge.Length; x++) { ge[x].OnSnapZoneExit(); } } } HeldItem = null; }
void checkGrabbableEvents() { // Bail if nothing in our trigger area if (grabsInTrigger == null) { return; } // If last closest was this one let event know and remove validator if (previousClosest != grabsInTrigger.ClosestGrabbable) { if (previousClosest != null) { // Fire Off Events GrabbableEvents[] ge = previousClosest.GetComponents <GrabbableEvents>(); if (ge != null) { for (int x = 0; x < ge.Length; x++) { ge[x].OnNoLongerClosestGrabbable(HandSide); } } previousClosest.RemoveValidGrabber(this); } // Update closest Grabbable if (grabsInTrigger.ClosestGrabbable != null && !HoldingItem) { // Fire Off Events GrabbableEvents[] ge = grabsInTrigger.ClosestGrabbable.GetComponents <GrabbableEvents>(); if (ge != null) { for (int x = 0; x < ge.Length; x++) { ge[x].OnBecomesClosestGrabbable(HandSide); } } grabsInTrigger.ClosestGrabbable.AddValidGrabber(this); } } if (grabsInTrigger.ClosestGrabbable != null && !HoldingItem) { grabsInTrigger.ClosestGrabbable.AddValidGrabber(this); } // Remote Grabbable Events // If last closest was this one, unhighlight object if (previousClosestRemote != grabsInTrigger.ClosestRemoteGrabbable) { if (previousClosestRemote != null) { // Fire Off Events GrabbableEvents[] ge = previousClosestRemote.GetComponents <GrabbableEvents>(); if (ge != null) { for (int x = 0; x < ge.Length; x++) { ge[x].OnNoLongerClosestRemoteGrabbable(HandSide); } } previousClosestRemote.RemoveValidGrabber(this); } // Update closest remote Grabbable if (grabsInTrigger.ClosestRemoteGrabbable != null && !HoldingItem) { // Fire Off Events GrabbableEvents[] ge = grabsInTrigger.ClosestRemoteGrabbable.GetComponents <GrabbableEvents>(); if (ge != null) { for (int x = 0; x < ge.Length; x++) { ge[x].OnBecomesClosestRemoteGrabbable(HandSide); } } grabsInTrigger.ClosestRemoteGrabbable.AddValidGrabber(this); } } // Set this as previous closest previousClosest = grabsInTrigger.ClosestGrabbable; previousClosestRemote = grabsInTrigger.ClosestRemoteGrabbable; }
public void GrabGrabbable(Grabbable grab) { // Grab is already in Snap Zone if (grab.transform.parent != null && grab.transform.parent.GetComponent <SnapZone>() != null) { return; } if (HeldItem != null) { ReleaseAll(); } HeldItem = grab; heldItemRigid = HeldItem.GetComponent <Rigidbody>(); // Mark as kinematic so it doesn't fall down if (heldItemRigid) { heldItemWasKinematic = heldItemRigid.isKinematic; heldItemRigid.isKinematic = true; } else { heldItemWasKinematic = false; } // Set the parent of the object grab.transform.parent = transform; // Set scale factor // Use SnapZoneScale if specified if (grab.GetComponent <SnapZoneScale>()) { _scaleTo = grab.GetComponent <SnapZoneScale>().Scale; } else { _scaleTo = ScaleItem; } // Is there an offset to apply? SnapZoneOffset off = grab.GetComponent <SnapZoneOffset>(); if (off) { offset = off; } else { offset = grab.gameObject.AddComponent <SnapZoneOffset>(); offset.LocalPositionOffset = Vector3.zero; offset.LocalRotationOffset = Vector3.zero; } // Lock into place if (offset) { HeldItem.transform.localPosition = offset.LocalPositionOffset; HeldItem.transform.localEulerAngles = offset.LocalRotationOffset; } else { HeldItem.transform.localPosition = Vector3.zero; HeldItem.transform.localEulerAngles = Vector3.zero; } // Disable the grabbable. This is picked up through a Grab Action disableGrabbable(grab); // Call Grabbable Event from SnapZone if (OnSnapEvent != null) { OnSnapEvent.Invoke(grab); } // Fire Off Events on Grabbable GrabbableEvents[] ge = grab.GetComponents <GrabbableEvents>(); if (ge != null) { for (int x = 0; x < ge.Length; x++) { ge[x].OnSnapZoneEnter(); } } if (SoundOnSnap) { VRUtils.Instance.PlaySpatialClipAt(SoundOnSnap, transform.position, 0.75f); } }