Detect collisions between LowLevelPhysics2D API objects
To detect when collisions occur and respond to them, use collision callback methods or event methods.
Read time 3 minutesLast updated 14 days ago
After you configure collisions, to detect when collisions occur and respond to them, use collision callback methods.
Follow these steps:
-
To enable collision callback methods, set theproperty of the physics world to
autoContactCallbacks.true -
Addto the list of implemented interfaces in your script. For example:
PhysicsCallbacks.IContactCallbackpublic class DetectCollisions : MonoBehaviour, PhysicsCallbacks.IContactCallback{ ...} -
Implement theand
OnContactBegin2Dmethods of the interface, and include the code you want to run when a collision starts and ends.OnContactEnd2D -
To activate the callback methods when a collision occurs, set theproperty of the shape to
contactEventsand thetrueproperty tocallbackTarget.this
For example, the following code creates a circle that logs when it collides with a larger circle:
using UnityEngine;using UnityEngine.LowLevelPhysics2D;// Add the PhysicsCallbacks.IContactCallback interfacepublic class DetectCollisions : MonoBehaviour, PhysicsCallbacks.IContactCallback{ void Start() { PhysicsWorld world = PhysicsWorld.defaultWorld; // Set the world to use automatic contact callbacks // Don't do this in production code. Configure the setting using a Physics Low Level Settings 2D asset instead. world.autoContactCallbacks = true; // Create a small falling circle PhysicsBody object1 = world.CreateBody(new PhysicsBodyDefinition { position = new Vector2(0.5f, 8f), type = PhysicsBody.BodyType.Dynamic }); PhysicsShape object1shape = object1.CreateShape(CircleGeometry.defaultGeometry); // Create a larger static circle below PhysicsBody object2 = world.CreateBody(new PhysicsBodyDefinition { position = new Vector2(0f, 0f), type = PhysicsBody.BodyType.Static }); object2.CreateShape(new CircleGeometry { radius = 3f, }); // Set object 1 to activate collisions object1shape.contactEvents = true; object1shape.callbackTarget = this; } // Log when the small circle collides public void OnContactBegin2D(PhysicsEvents.ContactBeginEvent eventData) { var contact = eventData.contactId.contact; Debug.Log("Collision started between shapes: " + contact.shapeA + " and " + contact.shapeB); } // Log when the small circle stops colliding public void OnContactEnd2D(PhysicsEvents.ContactEndEvent eventData) { var contact = eventData.contactId.contact; Debug.Log("Collision ended between shapes: " + contact.shapeA + " and " + contact.shapeB); }}
Detect a trigger overlap
A trigger shape doesn't collide with other colliders. Instead, other colliders pass through it. For more information, refer to Introduction to collision in the LowLevelPhysics2D API.
To detect when a trigger reports an overlap with another shape, follow these steps instead:
-
To enable trigger callback methods, set theproperty of the physics world to
autoTriggerCallbacks.true -
Addto the list of implemented interfaces in your script.
PhysicsCallbacks.ITriggerCallback -
Implement theand
OnTriggerBegin2Dmethods of the interface, and include the code you want to run when contact starts and ends.OnTriggerEnd2D -
To activate the callback methods when contact occurs, on both shapes set theproperty of the shape to
triggerEventsand thetrueproperty tocallbackTarget.this -
To enable the trigger, on the trigger shape, set theproperty to
isTrigger.true
For an example, refer to the example in the PhysicsCore2D repository on GitHub.
PhysicsShapeTriggerCallbackGet contacts without a callback
To get information about contact between objects without a callback, use the event APIs of the world instance. For example, to get collisions that have begun, use the method. For more information, refer to .
PhysicsWorld.contactBeginEventsPhysicsWorld