public void ForceUntouch(Grabbable grabbable) { if (_grabCandidates.ContainsKey(grabbable)) { _grabCandidates.Remove(grabbable); } }
protected virtual void OffhandGrabbed(Grabbable grabbable) { if (_grabbedObj == grabbable) { GrabbableRelease(Vector3.zero, Vector3.zero); } }
public (Grabbable, Collider) FindClosestGrabbable() { float closestMagSq = float.MaxValue; Grabbable closestGrabbable = null; Collider closestGrabbableCollider = null; foreach (Grabbable grabbable in _grabCandidates.Keys) { for (int j = 0; j < grabbable.GrabPoints.Length; ++j) { Collider grabbableCollider = grabbable.GrabPoints[j]; if (grabbableCollider == null) { continue; } Vector3 closestPointOnBounds = grabbableCollider.ClosestPointOnBounds(_gripTransform.position); float grabbableMagSq = (_gripTransform.position - closestPointOnBounds).sqrMagnitude; if (grabbableMagSq < closestMagSq) { closestMagSq = grabbableMagSq; closestGrabbable = grabbable; closestGrabbableCollider = grabbableCollider; } } } return(closestGrabbable, closestGrabbableCollider); }
public static void ClearAllGrabs(Grabbable grabbable) { foreach (var grabber in allGrabbers) { grabber.ForceUntouch(grabbable); grabber.ForceRelease(grabbable); } }
public void ForceGrab(Grabbable closestGrabbable, Collider closestGrabbableCollider) { GrabVolumeEnable(false); if (closestGrabbable != null) { Grab(closestGrabbable, closestGrabbableCollider); } }
public void ForceRelease(Grabbable grabbable) { bool canRelease = ( (_grabbedObj != null) && (_grabbedObj == grabbable || grabbable == null) ); if (canRelease) { GrabEnd(); } }
public void OnTriggerEnter(Collider otherCollider) { Grabbable grabbable = otherCollider.GetComponent <Grabbable>() ?? otherCollider.GetComponentInParent <Grabbable>(); if (grabbable == null) { return; } int refCount = 0; _grabCandidates.TryGetValue(grabbable, out refCount); _grabCandidates[grabbable] = refCount + 1; }
private void Grab(Grabbable closestGrabbable, Collider closestGrabbableCollider) { if (closestGrabbable.IsGrabbed) { closestGrabbable.GrabbedBy.OffhandGrabbed(closestGrabbable); } _grabbedObj = closestGrabbable; _grabbedObj.GrabBegin(this, closestGrabbableCollider); OnGrabStarted?.Invoke(_grabbedObj); Vector3 relPos = _grabbedObj.transform.position - transform.position; relPos = Quaternion.Inverse(transform.rotation) * relPos; _grabbedObjectPosOff = relPos; Quaternion relOri = Quaternion.Inverse(transform.rotation) * _grabbedObj.transform.rotation; _grabbedObjectRotOff = relOri; _lastPos = transform.position; _lastRot = transform.rotation; }
public void OnTriggerExit(Collider otherCollider) { Grabbable grabbable = otherCollider.GetComponent <Grabbable>() ?? otherCollider.GetComponentInParent <Grabbable>(); if (grabbable == null) { return; } bool found = _grabCandidates.TryGetValue(grabbable, out int refCount); if (!found) { return; } if (refCount > 1) { _grabCandidates[grabbable] = refCount - 1; } else { _grabCandidates.Remove(grabbable); } }
protected void GrabbableRelease(Vector3 linearVelocity, Vector3 angularVelocity) { _grabbedObj.GrabEnd(linearVelocity, angularVelocity); OnGrabEnded?.Invoke(_grabbedObj); _grabbedObj = null; }