unity-physicscore2d-filtering

Implements collision filtering, layer masks, and trigger areas in Unity PhysicsCore2D.

735|105|Updated Mar 4, 2017
One-click install
npx skills add https://github.com/Unity-Technologies/PhysicsExamples2D --skill unity-physicscore2d-filtering
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unity-physicscore2d-filtering
Source: https://github.com/Unity-Technologies/PhysicsExamples2D/tree/main/.claude/skills/unity-physicscore2d-filtering
Command: npx skills add https://github.com/Unity-Technologies/PhysicsExamples2D --skill unity-physicscore2d-filtering

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Controlling which 2D physics objects collide, ignore each other, or act as sensors in Unity's PhysicsCore2D API requires understanding contact filters, collision layers, and callback threading rules, which are easy to misconfigure.

Core Features & Use Cases

  • Contact Filter Callbacks: Implement IContactFilterCallback to suppress contacts at runtime, with guidance on thread-safe identity passing via PhysicsUserData.
  • Layer and Group Filtering: Configure PhysicsMask categories, collision groups, and faction-based rules for player, enemy, and projectile interactions.
  • Post-Simulate Filtering: Inspect contacts after the step using PhysicsShape.GetContacts() with predicate filters on normal angle and impulse magnitude.
  • Use Case: Build a game where same-team projectiles pass through each other but collide with enemies, using color-grouped PhysicsUserData and a custom contact filter callback.

Quick Start

Ask the assistant to write a PhysicsCore2D contact filter that prevents two dynamic circle bodies from colliding with each other while still colliding with the ground.

Frequently Asked Questions about unity-physicscore2d-filtering

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I filter collisions between specific objects in Unity PhysicsCore2D?

Implement PhysicsCallbacks.IContactFilterCallback and return false from OnContactFilter2D to suppress a contact. Enable contactFilterCallbacks on the shapes and world, and set the shape's callbackTarget to your MonoBehaviour.

How to pass object identity to a PhysicsCore2D contact filter callback?

Store identity in PhysicsUserData on the shape, such as an intValue group ID and boolValue participation flag. The callback can read userData from both shapes without touching managed MonoBehaviour state, which keeps it thread-safe.

Can PhysicsCore2D contact filter callbacks run off the main thread?

Yes, OnContactFilter2D can fire off the main thread and in parallel. Only perform read-only access to physics state, avoid writes and allocations, and prefer PhysicsUserData over reflection for identifying shapes.

What is the difference between contact filtering and post-simulate filtering in PhysicsCore2D?

Contact filtering suppresses contacts during the physics step via IContactFilterCallback. Post-simulate filtering subscribes to PhysicsEvents.PostSimulate and inspects produced contacts with PhysicsShape.GetContacts(), letting you react to contacts matching predicates like normal angle or impulse.

How do I make a trigger or sensor area in Unity PhysicsCore2D?

Mark shapes as sensors so they detect overlaps without producing collision response, then handle the overlap events for pickups, goal zones, or damage areas. Keep trigger geometry simple and unregister callbacks when the object is disabled.