/// <summary> Remove this script's reference to its SG_Material so that it is free to find another </summary> public void DetachScript() { TouchedCollider = null; TouchedObject = null; TouchedMaterialScript = null; if (TouchedDeformScript != null) { TouchedDeformScript.ResetMesh(); } TouchedDeformScript = null; ResetForces(); }
/// <summary> Calculate the force feedback levels based on material properties. </summary> private void UpdateFeedback() { if (TouchedObject != null) { Vector3 O = TouchedObject.transform.TransformPoint(entryOrigin); //O origin of collider on touch Vector3 E = TouchedObject.transform.TransformPoint(entryPoint); //E point where the collider touched the object Vector3 P = transform.position; //P current collider position if (debugDirections) { Debug.DrawLine(O, E); Debug.DrawLine(O, P); } Vector3 OE = (E - O).normalized; Vector3 OP = P - O; if (OP.magnitude > 0 && OE.magnitude > 0) { float cos = Vector3.Dot(OE, OP) / (/*OE.magnitude */ OP.magnitude); //removed OE.magnitude since it is normalized now. DistanceInCollider = OP.magnitude * cos; } else { DistanceInCollider = 0; } //we have calculated the distance, now for the material (if any is present) if (TouchedMaterialScript != null) { ForceLevel = TouchedMaterialScript.CalculateForce(DistanceInCollider, (int)this.handLocation); } //now for the deform script if it exists if (TouchedDeformScript != null && DistanceInCollider > 0) { Vector3 deformPoint = transform.position; // limits the position to go no further than the maximum. if (DistanceInCollider > TouchedDeformScript.maxDisplacement) { deformPoint = O + (OE * TouchedDeformScript.maxDisplacement); } TouchedDeformScript.AddDeformation(-OE, deformPoint, DistanceInCollider); } } }