Version: Unity 6.7 Beta (6000.7)
LanguageEnglish
  • C#

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

Cancel

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.

Build a PhysicsMask for the layer the object is on and another PhysicsMask for the layers it collides with. Pass both into a ContactFilter and assign it to PhysicsShapeDefinition.contactFilter before you create the shape. PhysicsLayers.GetLayerMask gets the layer mask whether the project uses GameObject layers or the Physics Core 2D API layers. Use PhysicsMask.All to represent all layers, or PhysicsMask.None to represent no layers.

Additional resources: PhysicsMask, PhysicsLayers.GetLayerMask, PhysicsShapeDefinition.contactFilter

 // 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); } }

Static Properties

Property Description
DefaultCategories The default categories used.
DefaultContacts The default contacts used.
defaultFilter Get a default contact filter that contacts everything.
Everything Get a contact filter that is all categories and contacts everything.

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.

Constructors

Constructor Description
PhysicsShape.ContactFilter Create a contact filter. See ContactFilterMode.

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.