Physics2D
Physics2D is the static class behind physics queries — "am I standing on the ground," "what's inside this explosion radius," "can this bullet reach that far." For everything else about 2D physics — the fixed timestep, colliders and sensors, collision/trigger callbacks, and collision layers — see the 2D Physics guide; this page is the reference for the query methods themselves.
Queries are legal from both OnUpdate and OnFixedUpdate, and always run against the current, already-simulated world state. None of them allocate memory, so calling one every frame (a ground check, say) never costs you a GC stutter.
layerMask on every method below defaults to 0, meaning "every layer." Build a mask by OR-ing together Physics2D.GetLayerMask results.
Physics2D.Raycast(Vector2 origin, Vector2 direction, float distance, out RaycastHit2D hit, ulong layerMask = 0)
The single most-used query in any platformer — a straight ray, stopping at the closest hit. Returns false (and leaves hit at its default) if nothing was hit.
if (Physics2D.Raycast(pos, Vector2.Down, 0.1f, out var hit, groundMask))
m_Grounded = true;Physics2D.RaycastAll(Vector2 origin, Vector2 direction, float distance, RaycastHit2D[] results, ulong layerMask = 0)
Every shape along the ray, not just the closest one — a piercing bullet's full hit list, for example. Fills the results array you pass in and returns the total number of matches, which may be more than results.Length — compare the return value against the array's length to know if you were truncated.
RaycastHit2D[] hits = new RaycastHit2D[8];
int count = Physics2D.RaycastAll(pos, dir, 20.0f, hits);
for (int i = 0; i < System.Math.Min(count, hits.Length); i++)
Log.Info($"Pierced: {hits[i].Entity?.Name}");Physics2D.OverlapCircle(Vector2 center, float radius, OverlapResult2D[] results, ulong layerMask = 0)
Every shape overlapping a circle — an explosion radius, for example. Same fill-a-buffer/return-the-count shape as RaycastAll above, but into an OverlapResult2D[] (no point/normal/fraction — an overlap query has no direction to measure them against).
OverlapResult2D[] nearby = new OverlapResult2D[16];
int count = Physics2D.OverlapCircle(explosionCenter, 3.0f, nearby, enemyMask);
for (int i = 0; i < System.Math.Min(count, nearby.Length); i++)
Log.Info($"Caught in the blast: {nearby[i].Entity?.Name}");Physics2D.OverlapBox(Vector2 center, Vector2 halfExtents, float rotation, OverlapResult2D[] results, ulong layerMask = 0)
Same as OverlapCircle above, but for an axis-aligned (or rotation-rotated) box. halfExtents is half the box's width/height, the same half-extent convention every collider in this engine uses.
OverlapResult2D[] results = new OverlapResult2D[8];
Physics2D.OverlapBox(doorwayCenter, new Vector2(1.0f, 0.5f), 0.0f, results);Physics2D.CircleCast(Vector2 origin, float radius, Vector2 direction, float distance, RaycastHit2D[] results, ulong layerMask = 0)
A "thick" ray — a circle swept from origin along direction. Use this instead of RaycastAll for anything too fast or thin for a plain ray to reliably hit (a small, fast projectile skipping past thin geometry between two frames).
RaycastHit2D[] hits = new RaycastHit2D[4];
Physics2D.CircleCast(pos, 0.2f, dir, 10.0f, hits);Physics2D.GetLayerMask(string layerName)
The bit for one named collision layer (set up in Project Settings → Physics Settings), or 0 if no project is open or no layer has that name. Combine layers with bitwise OR:
ulong mask = Physics2D.GetLayerMask("Enemy") | Physics2D.GetLayerMask("EnemyBullet");
Physics2D.Raycast(pos, dir, range, out var hit, mask);RaycastHit2D
What Raycast, RaycastAll, and CircleCast hand back.
| Member | Type | Meaning |
|---|---|---|
Point | Vector2 | World-space point where the ray/shape hit. |
Normal | Vector2 | Surface normal at the hit point. |
Fraction | float | 0 to 1 — how far along the cast's full distance the hit occurred. |
Entity | GameEntity? | The entity that was hit, or null. |
OverlapResult2D
What OverlapCircle and OverlapBox hand back — just the entity, since an overlap query has no single point, normal, or fraction the way a cast does.
| Member | Type | Meaning |
|---|---|---|
Entity | GameEntity? | The overlapping entity, or null. |