/// <summary> /// Checks the current grabbing gesture and tries to grab/release/approach a grabbable. /// This key method triggers the callbacks for the snapping system. /// </summary> /// <param name="prevFlex">Last grabbing gesture strength, normalised.</param> /// <param name="currentFlex">Current gragginb gesture strength, normalised.</param> protected void CheckForGrabOrRelease(float prevFlex, float currentFlex) { if (CheckGrabFailed(currentFlex)) { GrabFailed(); } else if (prevFlex < GrabFlexThreshold.y && currentFlex >= GrabFlexThreshold.y) { _nearGrab = false; GrabBegin(); } else if (prevFlex > GrabFlexThreshold.x && currentFlex <= GrabFlexThreshold.x) { GrabEnd(true); } if (_autoGrabStartTime.HasValue) { float autoGrabProgress = (Time.timeSinceLevelLoad - _autoGrabStartTime.Value) / AUTO_GRAB_TIME; if (autoGrabProgress < 1f) { OnGrabAttempt?.Invoke(_distantGrabAddress.snappable.gameObject, autoGrabProgress); } else { _nearGrab = false; GrabBegin(_distantGrabAddress.snappable.GetComponent <Grabbable>()); } } else { if (GrabbedObject == null && currentFlex > 0) { _nearGrab = true; NearGrab(currentFlex / GrabFlexThreshold.y); } else if (_nearGrab) { _nearGrab = false; NearGrab(0f); } } }
/// <summary> /// Triggers how close the grabber is to start grabbing a nearby object, informing the snapping system. /// </summary> /// <param name="factor">Current normalised value for the grab attemp, 1 indicates a grab.</param> protected void NearGrab(float factor) { if (factor == 0f) { OnGrabAttempt?.Invoke(null, 0f); return; } Grabbable closestGrabbable = FindClosestGrabbable(); if (closestGrabbable != null) { OnGrabAttempt?.Invoke(closestGrabbable.gameObject, factor); } else { OnGrabAttempt?.Invoke(null, 0f); } }