public void FixedUpdate() { if (Rigidbody) { _velocity = Rigidbody.velocity; } foreach (var tracker in _trackers) { if (!tracker.Update()) { Destroy(tracker.Joint); _cleanup.Add(tracker); IgnoreCollision(tracker.StabbedColliders, false); OnStabExit(tracker.Stabbable); } } for (var i = 0; i < _cleanup.Count; i++) { var tracker = _cleanup[i]; _trackers.Remove(tracker); StabbedObjects.Remove(tracker.StabbedObject); if (tracker.Stabbable) { StabbedStabbables.Remove(tracker.Stabbable); } } CleanupStabbableList(); _cleanup.Clear(); }
private void OnCollisionEnter(Collision collision) { if (_trackers.Count >= Settings.AllowedStabs) { return; } for (var i = 0; i < collision.contactCount; i++) { var contact = collision.GetContact(i); //check if the collided collider is one that causes stabbing var anyStabCollider = false; foreach (var stabCollider in StabbingColliders) { if (stabCollider == contact.thisCollider) { anyStabCollider = true; } } if (!anyStabCollider) { continue; } GameObject stabbedObject; HVRStabbable stabbable; HVRNotStabbable notStabbable; Collider[] stabbedColliders; var otherRB = contact.otherCollider.attachedRigidbody; if (otherRB) { stabbedObject = otherRB.gameObject; stabbable = stabbedObject.GetComponent <HVRStabbable>(); notStabbable = stabbedObject.GetComponent <HVRNotStabbable>(); //stabbedColliders = otherRB.GetColliders(otherRB.transform).ToArray(); stabbedColliders = GetColliders(otherRB, otherRB.transform).Where(e => !e.isTrigger).ToArray(); } else { stabbedObject = contact.otherCollider.gameObject; stabbable = stabbedObject.GetComponent <HVRStabbable>(); notStabbable = stabbedObject.GetComponent <HVRNotStabbable>(); stabbedColliders = stabbedObject.GetComponentsInChildren <Collider>().Where(e => !e.isTrigger).ToArray(); } if (StabbedObjects.Contains(stabbedObject)) { continue; } HVRStabbableSettings settings; if (stabbable) { settings = stabbable.Settings; } else { if (!StabAnything || StabAnything && notStabbable) { continue; } settings = FallbackSettings; } var stabDirection = StabLineWorld.normalized; var stabTransform = Tip; var dot = Vector3.Dot(StabLineWorld.normalized, -contact.normal); if (dot < Settings.AngleThreshold) { if (IsDualStabber) { var dt = Vector3.Distance(contact.point, Tip.position); var db = Vector3.Distance(contact.point, Base.position); if (dt < db || Vector3.Dot(StabLineWorld.normalized, contact.normal) < Settings.AngleThreshold) { if (LogFailedAngle) { Debug.Log($"stab failed dot product {dot}"); } continue; } stabTransform = Base; stabDirection = -stabDirection; } else { if (LogFailedAngle) { Debug.Log($"stab failed dot product {dot}"); } continue; } } if (collision.relativeVelocity.magnitude < settings.RequiredVelocity) { if (LogFailedVelocity) { Debug.Log($"stab failed velocity : {collision.relativeVelocity.magnitude}"); } continue; } Rigidbody.velocity = _velocity; if (stabbable && otherRB) { otherRB.velocity = stabbable.Velocity; //Debug.Log($"{stabbable.Velocity}"); } var joint = SetupStabJoint(settings, stabTransform, otherRB); //var joint = SetupStabJoint(settings, contact.point, otherRB); StabbedObjects.Add(stabbedObject); if (stabbable) { StabbedStabbables.Add(stabbable); } IgnoreCollision(stabbedColliders); var tracker = new HVRStabTracker(this, stabbable, settings, joint, stabbedObject, stabDirection, stabTransform, stabbedColliders ); _trackers.Add(tracker); OnStabEnter(stabbable, collision); } }