OnCollisionExit(Collision)
OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
Read time 3 minutesLast updated 3 days ago
Definition
- Type: Method
- Namespace: UnityEngine
OnCollisionExit()
public void OnCollisionExit(Collision collision)
Parameters
Remarks
OnCollisionExitcollisionOnCollisionExitcollisionNote: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. Collision events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
If one of the objects involved in the collision is deactivated or destroyed before the objects separate, isn't called. Physics backends report collision enter, stay, and exit, so Unity sends when the two colliders separate. Deactivation and destruction don't produce an exit message, for two different reasons:
OnCollisionExitOnCollisionExit- If you disable a collider, or the GameObject it belongs to, the message would report a collider that is no longer in the simulation. Deactivating a GameObject also stops its scripts from receiving messages at all, unlike disabling a , which still receives collision events.
MonoBehaviour - If you destroy a collider, there is nothing left to report to. The destroyed object can't receive the message, and the Collision that supplies describes a collider that no longer exists.
OnCollisionExit
Unity could send in these cases by storing and maintaining a record of every touching pair in the scene, but maintaining that record costs CPU time and memory in every project, including projects that don't need this behavior.
OnCollisionExitIf your logic depends on being called whenever contact ends, including when an object is deactivated or destroyed, you need to implement additional logic, such as handling cleanup in MonoBehaviour.OnDisable() or MonoBehaviour.OnDestroy(), or recording contacts yourself and reconciling them each physics step. If you record contacts yourself, record each collider's EntityId, and any data you need about it, while the collider is still alive: after Unity destroys a collider, the reference to it compares equal to , so you can't read any values from it. Subscribe to Physics.ContactEvent to read contacts from each simulation step, rather than relying on per-object messages. Unity doesn't report every contact in the scene through this event: a collider contributes contacts only if you set Collider.providesContacts to on it, or it has a MonoBehaviour with an method attached, so enable on the colliders you want to track. Each ContactPair identifies its colliders by ContactPair.colliderEntityId and ContactPair.otherColliderEntityId, so you can match contacts against your own records. For more information, refer to OnCollision events.
OnCollisionExitnulltrueOnCollisionStayprovidesContactsOnCollisionExitExamples
using UnityEngine;using System.Collections;public class Example : MonoBehaviour{ void OnCollisionExit(Collision other) { print("No longer in contact with " + other.transform.name); }}