void OnTriggerExit(Collider col) { SG_BasicFeedback touch = col.GetComponent <SG_BasicFeedback>(); if (touch && touch.feedbackScript && touch.feedbackScript.TrackedHand) { //SG_Debugger.Log("Collider Exits"); int scriptIndex = this.HandModelIndex(touch.feedbackScript.TrackedHand); if (scriptIndex < 0) { //SG_Debugger.Log("Something went wrong with " + this.gameObject.name); //it is likely the palm collider. } else { //belongs to an existing SenseGlove. if (ValidScript(touch)) { this.detectedHands[scriptIndex].RemoveCollider(touch); if (this.detectedHands[scriptIndex].TotalColliders <= 0) { //raise release event. //SG_Debugger.Log("Escape!"); if (detectedHands[scriptIndex].EventFired && !(this.singleGlove && this.detectedHands.Count > 1)) //only fire if the last glove has been removed. { this.FireRemoveEvent(this.detectedHands[scriptIndex].TrackedHand); } this.RemoveEntry(scriptIndex); } } } } }
/// <summary> Register that a collider of this hand has been removed from the zone. </summary> /// <param name="touch"></param> public void RemoveCollider(SG_BasicFeedback touch) { if (touch.handLocation == SG_HandSection.Unknown) { OtherColliders -= 1; } else if (touch.handLocation == SG_HandSection.Wrist) { WristColliders -= 1; } else { int index = (int)touch.handLocation; FingerColliders[index] -= 1; } TotalColliders -= 1; }
/// <summary> Called by SG_Feedback when it touches an interactable. Informs this Interactable that it is no longer being touched </summary> /// <param name="touchScript"></param> public void UnTouchedBy(SG_BasicFeedback touchScript) { if (touchScript != null && touchScript.linkedGlove != null) { int GI = GetTouchIndex(touchScript.linkedGlove); if (GI > -1) { this.touchedColliders[GI] = this.touchedColliders[GI] - 1; //++ does not work reliably... if (this.touchedColliders[GI] < 1) //less than one remaining { this.touchedScripts.RemoveAt(GI); this.touchedColliders.RemoveAt(GI); if (this.touchedScripts.Count < 1) //reduced the amount of touchedScripts to 0 { this.OnUnTouched(); } } } } }
/// <summary> Called by SG_Feedback when it touches an interactable. Informs this Interactable that it is being touched. </summary> /// <param name="touchScript"></param> public void TouchedBy(SG_BasicFeedback touchScript) { if (touchScript != null && touchScript.linkedGlove != null) { int GI = GetTouchIndex(touchScript.linkedGlove); if (GI > -1) { this.touchedColliders[GI] = this.touchedColliders[GI] + 1; //++ does not work reliably... } else { this.touchedScripts.Add(touchScript.linkedGlove); this.touchedColliders.Add(1); if (this.touchedColliders.Count == 1) // Only for the first new script. { this.OnTouched(); } } } }
//-------------------------------------------------------------------------------------------------------------------------- // Collision Detection #region Collision void OnTriggerEnter(Collider col) { SG_BasicFeedback touch = col.GetComponent <SG_BasicFeedback>(); if (touch && touch.feedbackScript && touch.feedbackScript.TrackedHand) //needs to have a grabscript attached. { int scriptIndex = this.HandModelIndex(touch.feedbackScript.TrackedHand); //#1 - Check if it belongs to a new or existing detected glove. if (scriptIndex < 0) { if (ValidScript(touch)) { //SG_Debugger.Log("New Grabscript entered."); this.AddEntry(touch.feedbackScript.TrackedHand); scriptIndex = this.detectedHands.Count - 1; this.detectedHands[scriptIndex].AddCollider(touch); } } else { if (ValidScript(touch)) { //SG_Debugger.Log("Another collider for grabscript " + scriptIndex); this.detectedHands[scriptIndex].AddCollider(touch); } } //if no time constraint is set, raise the event immediately! if (this.activationTime <= 0 && scriptIndex > -1 && scriptIndex < detectedHands.Count && this.detectedHands[scriptIndex].TotalColliders == this.activationThreshold) { //SG_Debugger.Log("ActivationThreshold Reached!"); if (!(detectedHands[scriptIndex].EventFired) && !(this.singleGlove && this.detectedHands.Count > 1)) { this.detectedHands[scriptIndex].EventFired = true; this.FireDetectEvent(this.detectedHands[scriptIndex].TrackedHand); } } } }
/// <summary> Returns true if this is a valid script. </summary> /// <param name="touch"></param> /// <returns></returns> private bool ValidScript(SG_BasicFeedback touch) { return(this.detectionMethod == DetectionType.AnyFinger || (this.detectionMethod == DetectionType.SpecificFingers && this.ValidScript(touch.handLocation))); }