…beginning counter drag operation
Collision callbacks for scripts
When collisions occur, the physics engine calls functions with specific names on any scripts attached to the objects involved. You can place any code you like in these functions to respond to the collision event. For example, you might play a crash sound effect when a car bumps into an obstacle.
On the first physics update where the collision is detected, the
OnCollisionEnter function is called. During updates where contact is maintained,
OnCollisionStay is called and finally,
OnCollisionExit indicates that contact has been broken. Trigger colliders call the analogous
OnTriggerExit functions. Note that for 2D physics, there are equivalent functions with 2D appended to the name, eg,
OnCollisionEnter2D . Full details of these functions and code samples can be found on the Script Reference page for the MonoBehaviour class.
With normal, non-trigger collisions, there is an additional detail that at least one of the objects involved must have a non-kinematic Rigidbody (ie, Is Kinematic must be switched off). If both objects are kinematic Rigidbodies then
OnCollisionEnter , etc, will not be called. With trigger collisions, this restriction doesn’t apply and so both kinematic and non-kinematic Rigidbodies will prompt a call to
OnTriggerEnter when they enter a trigger collider.