unity-2d-physics

v2026.09.24

Unity 2D physics — Rigidbody2D body types, Collider2D shapes, joints, effectors, layer collision matrix, contact filters, IEEE physics gotchas in 2D, raycast/overlap APIs. USE WHEN: tuning Rigidbody2D movement, joint setups (hinge/spring/distance/ slider), one-way platforms, conveyor belts, area effects, layered collisions, raycast queries. DO NOT USE FOR: 3D physics (use `unity-physics-anim`); platformer character movement (use `unity-2d-gameplay`); tilemap collision (use `unity-2d-tilemap`).

GitHub
Install command
npx skhub add claude-dev-suite/unity-2d-physics
Markdown
SKILL.md

Unity 2D Physics

Rigidbody2D body types

Body TypeBehaviour
DynamicMoves under force/gravity/velocity. Most gameplay objects.
KinematicMoves only via MovePosition/MoveRotation. No automatic forces. Use for player controllers when you handle motion manually.
StaticDoesn't move. Optimised. Use for terrain, tilemaps.
[RequireComponent(typeof(Rigidbody2D))]
public class Push : MonoBehaviour {
    private Rigidbody2D _rb;
    void Awake() {
        _rb = GetComponent<Rigidbody2D>();
        _rb.interpolation = RigidbodyInterpolation2D.Interpolate;
        _rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;  // critical for fast movers
    }
    public void Push2D(Vector2 dir) => _rb.AddForce(dir * 10f, ForceMode2D.Impulse);
}

Collider2D shapes

BoxCollider2D, CircleCollider2D, CapsuleCollider2D, EdgeCollider2D (open polyline), PolygonCollider2D (closed shape). Stick to primitives where possible — polygons with many vertices are expensive.

CompositeCollider2D merges multiple child colliders into a single optimized shape (standard for tilemap collision).

Joints

JointUse
Hinge2DDoors, windmills, ragdoll limbs
Distance2DTwo bodies at fixed distance — chains, ropes (with multiple links)
Spring2DBouncy connections — suspension, jelly
Slider2DConstrained linear motion — drawers, pistons
Fixed2DGlue bodies together until break force
Wheel2DVehicle wheels (suspension + motor)
Target2DMove toward a target with damping (mouse drag)
Friction2DDampen relative motion (handy on conveyor return paths)
Relative2DAnchor at a relative offset

Effectors

Auto-applied behaviours on a collider:

EffectorEffect
PlatformEffector2DOne-way platforms, flip-through detection
AreaEffector2DConstant force inside the area (wind, water current)
PointEffector2DAttract/repel from a point
SurfaceEffector2DConveyor belt — moves objects in contact
BuoyancyEffector2DWater buoyancy + drag

Tag the collider as Used By Effector and add the effector component on the same GameObject.

Layer collision matrix

Edit > Project Settings > Physics 2D > Layer Collision Matrix. Cull pairs (Player/Player, EnemyProjectile/Enemy) — both correctness and perf.

Contact filters & queries

private static readonly Collider2D[] HitsBuffer = new Collider2D[16];
private ContactFilter2D _filter;

void Awake() {
    _filter = new ContactFilter2D {
        useLayerMask = true,
        layerMask = LayerMask.GetMask("Enemy"),
        useTriggers = false,
    };
}

void Hit() {
    int n = Physics2D.OverlapCircle(transform.position, 0.5f, _filter, HitsBuffer);
    for (int i = 0; i < n; i++) HitsBuffer[i].GetComponent<IDamageable>()?.Damage(10);
}

Allocation-free OverlapCircle/Box/Raycast overloads avoid GC pressure in hot paths.

2D physics gotchas

  • Z position affects rendering but NOT 2D physics (everything is at z=0 collision-wise).
  • Mass matters for collision response; tweak via Rigidbody2D Mass (or Auto Mass = ON for density-based).
  • Continuous Collision Detection — required for high-speed objects (bullets) to avoid tunneling.
  • Simulation Speed: 50Hz default (FixedUpdate). Higher → smoother + heavier; 30Hz acceptable for casual.
  • Z-axis Job — physics 2D doesn't run jobs by default; enable in Project Settings → Physics 2D > Use Multithreaded Simulation.

Anti-patterns

Anti-patternFix
transform.position for Rigidbody2D objectsMovePosition / set linearVelocity in FixedUpdate
Polygon collider with hundreds of pointsDecompose into primitives or composite
Discrete collision on bulletsContinuous CCD
Many per-frame OverlapCircle allocationsUse NonAlloc / ContactFilter2D overloads
Joints with default break force = infinity for breakable linksSet Break Force / Break Torque
Effector without Used By Effector flagTick Used By Effector on the collider

Production checklist

  • Layer collision matrix culled
  • Static colliders + composite for tilemap-like geometry
  • Continuous detection on fast bodies
  • Allocation-free physics queries in hot paths
  • Joints have realistic break forces
  • Physics 2D substep sufficient for target framerate
Discovery
Tags

No tags published for this skill.

Version
Latest version metadata

Version

v2026.09.24

Published

Sep 24, 2026

Category

Uncategorized

License

MIT

Source path

skills/gamedev/unity-2d-physics

Default branch

main

Latest commit

9496306

Tree SHA

fe4e2f1