ContactFilter
struct in
Unity.U2D.Physics
Suggest a change
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Close
Submission failed
For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close
Description
A contact filter is used to control what contacts are created when intersecting other shapes.
A contact filter contains a filter with the addition of a group index allowing overrides to the filter.
See ContactFilterMode.
// The following example sets a falling circle to be on the MyNewLayer layer
// and collide only with other MyNewLayer layer objects. Create a Physics Core
// Settings 2D asset, enable Use Physics Layers, and add a layer called MyNewLayer
// before you 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);
}
}
Properties
| Property |
Description |
| categories |
The categories this object is in.
Usually you would only set one bit but multiple are allowed.
|
| contacts |
The categories this object will produce contacts with.
|
| groupIndex |
The group which the contact filter uses to determine if the categories and contact masks are used.
See ContactFilterGroupMode for more information.
|
Public Methods
| Method |
Description |
| CanContact |
Will this contact filter produce a contact with the specified contact filter.
The term "contact" here means that if these filters were used on two PhysicsShape, would a contact be produced.
|