OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
public void OnCollisionExit(Collision collision)
receives an instance of the
Collision class as the
parameter. This parameter contains information about the collision such as contact points and impact velocity. If you don't use this collision data in your
callback, leave out the
parameter from the method declaration to avoid unnecessary calculations.
Note: 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: - 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. - 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. 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. If 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.
can be a
coroutine.
using UnityEngine;using System.Collections;public class Example : MonoBehaviour{ void OnCollisionExit(Collision other) { print("No longer in contact with " + other.transform.name); }}