// Called when a collider exits the trigger collider protected virtual void OnTriggerExit(Collider other) { // Get a reference to the damageable DamageReceiver damageReceiver = other.GetComponent <DamageReceiver>(); if (damageReceiver != null) { // Ignore if still inside the designated trigger collider's radius if (scannerTriggerCollider != null) { Vector3 colliderWorldPos = scannerTriggerCollider.transform.TransformPoint(scannerTriggerCollider.center); float distanceToTarget = Vector3.Distance(damageReceiver.GetClosestPoint(colliderWorldPos), colliderWorldPos); if (distanceToTarget > scannerTriggerCollider.radius) { // Remove the damageable from the list int index = damageReceiversInRange.IndexOf(damageReceiver); if (index != -1) { damageReceiversInRange.RemoveAt(index); onDamageReceiverUndetected.Invoke(damageReceiver); } } } } }
// Called when a collider enters a trigger collider. protected virtual void OnTriggerEnter(Collider other) { DamageReceiver damageReceiver = other.GetComponent <DamageReceiver>(); if (damageReceiver != null) { // Ignore if outside the designated trigger collider's radius if (scannerTriggerCollider != null) { Vector3 colliderWorldPos = scannerTriggerCollider.transform.TransformPoint(scannerTriggerCollider.center); float distanceToTarget = Vector3.Distance(damageReceiver.GetClosestPoint(colliderWorldPos), colliderWorldPos); if (distanceToTarget < scannerTriggerCollider.radius) { if (!damageReceiversInRange.Contains(damageReceiver)) { // Add the damageable to the list and invoke the event damageReceiversInRange.Add(damageReceiver); onDamageReceiverDetected.Invoke(damageReceiver); } } } } }