Configure collisions between Physics Core 2D API objects
Change which objects collide with which using layers and masks, and enable using 64 layers instead of 32.
Read time 4 minutesLast updated 13 days ago
In the Physics Core 2D API, collisions are enabled by default.
By default the following applies:
- Objects use GameObject layers, and all objects are on the built-in layer called Default.
- Objects collide with objects on all other layers.
- You can use up to 32 layers.
To change this behaviour, do the following:
- Set the Physics Core 2D API to use its own set of 64 layers, instead of GameObject layers.
- Configure which layers objects are on, and create layer masks that set which layers an object interacts with.
Set the API to use its own set of layers
Follow these steps:
- Create and assign a Physics Core Settings 2D asset. For more information, refer to Configure global 2D physics settings.
- In the window, select PhysicsCore 2D > Settings.Project Settings
?
- In the Global section, enable Use Physics Layers.
The API now uses the layers in the Layers section of the Settings window.
PhysicsMaskUse the Layers tab to add, edit, or remove layers. For more information, refer to PhysicsCore 2D reference.
Change which objects collide in the Editor
Follow these steps:
-
Create the layers you need. For more information, refer to PhysicsCore 2D reference.
-
Create a physics object with a publicand attach it to a GameObject.
PhysicsShapeDefinition -
Select the GameObject, then in thewindow open the Contact Filter dropdown.Inspector
?
-
Set Categories to the layers you want the object to be on. For example, the first built-in layer called Default.
-
Set Contacts to the layers you want the object to collide with. For example Nothing for no layers.
Change which objects collide in a script
Follow these steps:
-
Create the layers you need. For more information, refer to PhysicsCore 2D reference.
-
Create aobject with the layer you want the object to be on. For example:
PhysicsMaskPhysicsMask objectLayer = PhysicsLayers.GetLayerMask("Car");gets the layer mask whether you're using GameObject layers or the Physics Core 2D API layers.PhysicsLayers.GetLayerMask -
Create anotherobject with the layers you want the object to collide with.
PhysicsMaskPhysicsMask objectLayer = PhysicsLayers.GetLayerMask("Walls"); -
Create aobject with the two physics layer masks. For example:
ContactFilterPhysicsShape.ContactFilter myContactFilter = new PhysicsShape.ContactFilter { categories = objectLayer, contacts = contactLayer },You can also useto represent all layers, orPhysicsMask.Allto represent no layers.PhysicsMask.None -
Assign theto the
ContactFilterwhen you create it. For example:PhysicsShapePhysicsShapeDefinition shapeDefinition = new PhysicsShapeDefinition{ contactFilter = myContactFilter};
If is a bitmask rather than a set of layers, add the attribute. Unity then displays the mask as bit values.
PhysicsMaskPhysicsMask.ShowAsPhysicsMaskFor more information, refer to .
PhysicsMaskPrevent objects passing through each other
Because physics bodies move in steps, fast-moving objects can sometimes pass through other objects without activating a collision. This is called tunnelling.
The 2D physics system automatically uses continuous collision detection to prevent tunnelling when dynamic objects approach static objects. However if a very fast-moving object still passes through another object, enable the property of the physics body to force continuous collision detection. Enabling this setting can reduce performance.
fastCollisionsAllowedExample
The following example sets a falling circle to be on the MyNewLayer layer and collide only with other MyNewLayer layer objects. It passes through the larger circle that's in the default layer.
To use the example:
- Create a Physics Core Settings 2D asset, enable Use Full Layers, and add a layer called MyNewLayer.
- Attach the script to a GameObject in your scene, and enter Play mode.
using UnityEngine;using Unity.U2D.Physics;public class ContactFilters : MonoBehaviour{ void Start() { CircleGeometry smallCircleShape = new CircleGeometry{ radius = 0.5f }; CircleGeometry largeCircleShape = new CircleGeometry{ radius = 3f }; PhysicsWorld world = PhysicsWorld.defaultWorld; // Create a small falling circle PhysicsBody object1 = world.CreateBody(new PhysicsBodyDefinition { position = new Vector2(0.5f, 8f), type = PhysicsBody.BodyType.Dynamic }); // Create a shape on the MyNewLayer layer, which only collides with the MyNewLayer layer PhysicsMask objectLayer = PhysicsLayers.GetLayerMask("MyNewLayer"); PhysicsMask collisionLayer = PhysicsLayers.GetLayerMask("MyNewLayer"); PhysicsShape.ContactFilter contactFilter = new PhysicsShape.ContactFilter { categories = objectLayer, contacts = collisionLayer }; PhysicsShapeDefinition smallCircleDefinition = new PhysicsShapeDefinition{ contactFilter = contactFilter }; object1.CreateShape(smallCircleShape, smallCircleDefinition); // Create a larger static circle below, on the default layer PhysicsBody object2 = world.CreateBody(new PhysicsBodyDefinition { position = new Vector2(0f, 0f), type = PhysicsBody.BodyType.Static }); object2.CreateShape(largeCircleShape); }}