private void OnTriggerStay(Collider other) { // If you don't plan on using degredation over distance, you can // remove OnTriggerStay entirely and lighten the load. dragger = GetComponent <FrameDragger>(); FrameDragReceiver receiver = other.GetComponent <FrameDragReceiver>(); if (receiver != null && receiver.isActiveAndEnabled) { if (degradeByDistance) { // A full dragMagnitude (1.0) means the receiver almost // perfectly matches the dragger's rotation and velocity. // The magnitude should be full if the receiver is within // the dragEffectRadius of the dragEffectOrigin.position, // and degrade the further the object is from that radius. // // There are many ways to do this, and all of them should // be smarter than this. float receiverDistance = Vector3.Distance(receiver.transform.position, dragEffectOrigin.position); receiver.dragMagnitude = Mathf.Clamp01(dragEffectRadius / receiverDistance); } else { receiver.dragMagnitude = 1.0f; } } }
private void OnTriggerExit(Collider other) { FrameDragReceiver receiver = other.GetComponent <FrameDragReceiver>(); // We have to track all dragged objects because trigger colliders // detect collisions even when the trigger's object is disabled. // If we don't track this, receivers of a disabled dragger retain // modified velocities; they're effectively still dragged as if the // object wasn't disabled. :( Thanks, Unity! if (receiver != null && receiver.isActiveAndEnabled) { bool listContainsItem = dragged.Contains(receiver); // Remove a receiver from the list of dragged objects only if // it's not already on the list, and only on exit. if (listContainsItem) { dragged.Remove(receiver); } } }