Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions skills/create-2d-physics/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Security notes: create-2d-physics

This skill has the agent run C# inside the user's open Unity Editor through `unity command eval`. Automated skill scanners flag that as a powerful capability. It is intentional, and it is limited by the safeguards below.

## Accepted risks

| Risk | Capability | Why it is accepted |
|---|---|---|
| `SEC_POWER_CAP` | Runs C# in the user's open Editor through `unity command eval` | It only reaches the Editor the user already has open, on their own machine, as that user, so it grants nothing they couldn't do themselves. `eval` sits behind the Pipeline capability gate. No code fetched from a remote source is run. Where a named `unity command` covers a step, the skill uses that instead of `eval`. |

## Mitigations

- **Only the user's own Editor.** `unity command eval` talks to the Editor open on this machine. It can't reach another machine or another user's Editor.
- **Capability gate.** `eval` is only available when the project's Pipeline package provides it.
- **No remote code.** The agent runs C# it writes from this skill's own recipes. Nothing downloaded from outside is executed.
- **Named commands first.** When a dedicated `unity command` covers a step, the skill uses it instead of `eval`.
78 changes: 78 additions & 0 deletions skills/create-2d-physics/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
name: create-2d-physics
description: "Creates and fixes Unity 2D physics across both systems: 2D Physics Core and the legacy Rigidbody 2D system. Make sure to use this skill for any 2D physics question, even if the user doesn't mention either system by name. Not for 3D physics."
allowed-tools: WebFetch, WebSearch
metadata:
version: 0.3.0
support: "https://unity.com/support-services"
---

# Create and fix Unity 2D physics

Do not weigh 3D physics options. If evidence shows the user means 3D, say so and stop.

Prefer `WebFetch` over `WebSearch` — faster and lands on the exact reference. Only fetch what you need. Replace `<VERSION>` with the following before fetching:
- **docs.unity.com**: The Unity version, e.g. `6000.7`. Pages are markdown — URLs end in `.md`.
- **Package on docs.unity3d.com**: The package version, e.g. `@1.1`. Check `Packages/manifest.json` if unsure.

## Important

- Use the unity cli (`unity command --format json`) and the `unity-cli` and `unity-pipeline` skills for each step.
- Don't use `using` directives or unqualified types in the `snippet` for `eval`. The compiler reads using `UnityEngine` and a bare AssetDatabase, Volume, or Object causes errors (CS0246 / CS0103 / CS0104). `return` what you want to read; it arrives at `data.result.result`.
- Create a restore point you can roll back to if your changes fail.
- Do only what's asked. Don't change unrelated assets or files.
- Avoid long explanations.
- Never leave verification code in the user's script; never build logging or settle-detection they didn't ask for. See [Final step](#final-step).
- Never reflect over types to discover an API — fetch the member page instead. The system probe is the one exception.

## Step 0: Which physics system

Unity has two 2D physics systems. They share no code and never interact. Identify which one before writing any physics code — an answer from the wrong system compiles, runs, and does nothing.

Check the request, scene and existing scripts against the table below. Components are the reliable signal — check scene and existing scripts first. Check the user's request wording too: "add a collider" is component language, "spawn a thousand" is script language.

| Signal | System |
| --- | --- |
| Physics Pose, Area, Constraint or Simulation components | Core components, 6000.7+ with the package |
| Rigidbody 2D, Collider 2D, Joint 2D, Effector 2D | Legacy |
| `Unity.U2D.Physics` or `UnityEngine.LowLevelPhysics2D` types in script | Core in script |
| `Rigidbody2D`, `Collider2D`, `Physics2D` types | Legacy |
| Both present | Separate simulations. Say so, treat separately |
| No signal | Stop and ask, offering the three routes above |

Key:
- Legacy = Rigidbody 2D and Collider 2D components.
- Core components = Physics Pose and Physics Area, requiring `com.unity.2d.physics` and Unity 6000.7+.
- Core in script = `Unity.U2D.Physics` API, no package needed.

If still unsure which systems are installed, run the probe. If the route is still unknown, stop and ask — offer all three routes and never choose for the user:

Probe:
```
unity command eval --code 'var r = ""; foreach (var n in new[] { "UnityEngine.Rigidbody2D", "Unity.U2D.Physics.PhysicsWorld", "UnityEngine.LowLevelPhysics2D.PhysicsWorld", "Unity.U2D.Physics.PhysicsPose" }) { var f = false; foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies()) if (a.GetType(n) != null) f = true; r += f + ","; } return r;'
```
The Probe returns: legacy present, Core as `Unity.U2D.Physics`, Core as `UnityEngine.LowLevelPhysics2D`, Core components present. Core exists if either Core value is true; the true one gives the namespace. `false` rules that route out.

## System reference

Once you know the route, read the matching reference before acting:
- [2D Physics Core reference](references/2d-physics-core.md) - components, configuring them, creating bodies in script, ownership, and the full Core topic map.
- [Legacy 2D physics reference](references/legacy-2d-physics.md) - the legacy manual topic map.

## Worked examples

Prefer in order: the C# example on the type or member page you are using; existing project scripts (match their style); the sample project at `https://github.com/Unity-Technologies/PhysicsExamples2D`. Check all three before inventing a pattern.

## Final step

Do the following checks:

1. Check the project has zero console errors. Use `console_status` for counts.
2. Stop there unless the user asked you to prove it works or you suspect a specific fault.
3. If you do enter Play mode, do it once only to chase a fault you already suspect. Read the world through `eval` rather than adding logging. Take no screenshots as they take too much time.

If a check fails, go back and reread the docs pages in the matching reference to find out what you missed.

## Final report

Short checklist: what you changed and why, which physics system you used, anything left for the user to decide or do.
142 changes: 142 additions & 0 deletions skills/create-2d-physics/references/2d-physics-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# 2D Physics Core

- [Step 0](#step-0) — when to read before acting
- [Configuring a component](#configuring-a-component)
- [Creating in script](#creating-in-script)
- [Ownership](#ownership)
- [What you are likely to get wrong](#what-you-are-likely-to-get-wrong)
- [Manual: 2D Physics Core](#manual-2d-physics-core)
- [Scripting reference](#scripting-reference)
- [Package documentation](#package-documentation)

The engine API is a built-in module — no package needed. The Physics Pose, Area and Constraint **components** come from `com.unity.2d.physics`, requiring 6000.7+. Check `Packages/manifest.json` before suggesting a component; if absent, offer to add it using the `unity-package-management` skill.

A component is not the physics object. It owns a body, shape or joint — operate on that owned object. Read/write it, apply forces, query it, handle contacts (from a worker thread if needed). See [Physics Pose and Physics Area components](https://docs.unity.com/en-us/engine/<VERSION>/manual/unity2d/2d-physics-api/create-objects/pose-and-area.md).

The components use only the public API, by design, so nothing is component-only — a plain MonoBehaviour driving the engine API always works. Never say a case is unsupported. For many shapes or nested areas, Physics Area Composite already exists.

Within Core: components for scene-authored objects, script for bulk runtime creation. The modules `com.unity.modules.physics2d` and `com.unity.modules.physicscore2d` are on by default; only matter if stripped. Version sets the namespace, not which system exists — the [Unity 6.5 upgrade guide](https://docs.unity3d.com/<VERSION>/Documentation/Manual/UpgradeGuideUnity65.html) covers the rename. Only the components require 6000.7.

## Step 0

Before you go forward with a workflow or diagnosis:

1. Open and read the documentation page(s) from the [Manual](#manual-2d-physics-core) or [Scripting reference](#scripting-reference) that cover the specific subject related to what you intend to do or fix and check if you've missed anything.
2. Name the file(s) you read in your response.

This is because your training knowledge about Unity might be out of date, incorrect, or for the wrong Unity version.

Always use versioned URLs — unversioned links resolve to the current release, not the project's version.

Core script type pages are an exception: the API only changed namespace, so the version only decides whether to write `Unity.U2D.Physics` or `UnityEngine.LowLevelPhysics2D`. Use the project's version when known, otherwise 6000.7.

Most tasks (apply force or impulse, move a body, read velocity, add a component) need no reading — just do it.

Exceptions that always require reading first:
- **Setting component values**: fetch the type page per [Configuring a component](#configuring-a-component).
- **Creating a body in script**: read the two pages in [Creating in script](#creating-in-script).
- **Any member you cannot name with confidence**: read one page. Never read more than two pages for a single task.

## Configuring a component

Follow these rules for configuring a component:

- Never write serialized fields — the serialized names differ from public names. Drive everything through public properties and methods via `eval`.
- Definition and geometry are separate: a pose's body settings live in its definition; an area's shape is its geometry. Configure them independently. A change to either needs its apply call before it takes effect — the type page states which one.
- Before configuring any component, fetch its API page and use only members it lists. Read the installed version from `Packages/manifest.json` (major.minor, e.g. `1.1`), then fetch:
`https://docs.unity3d.com/Packages/com.unity.2d.physics@<version>/api/Unity.U2D.Physics.<Type>.html`

One page per type covers all members. If a member is missing, treat it as undocumented, not absent.

## Creating in script

Before creating a Core body in script, read these two pages and follow their examples. Set `type` on the definition explicitly.

- `https://docs.unity.com/en-us/engine/<VERSION>/script-reference/unity/u2d/physics/physicsbodydefinition.md`
- `https://docs.unity.com/en-us/engine/<VERSION>/script-reference/unity/u2d/physics/physicsbody.md`

Use the project's version, or 6000.7 if unknown. On 6000.3–6000.4 the namespace is `UnityEngine.LowLevelPhysics2D`, e.g. `.../script-reference/unityengine/lowlevelphysics2d/physicsbody.md`.

Script types — joints, geometry, queries, and everything else — are engine types in the main scripting reference only. Never look for them in the `com.unity.2d.physics` package docs.

## Ownership

Core only. Applies to worlds, bodies, shapes, chains and joints.

Owner-gated calls state it on their page, as [PhysicsBody.Destroy](https://docs.unity.com/en-us/engine/<VERSION>/script-reference/unity/u2d/physics/physicsbody/destroy.md) does. The owner key argument defaults to zero (matches objects with no owner); `SetOwner` is different — there, zero creates a new key. A refusal logs a warning rather than throwing.

Create a key and own what you create. Queries return objects you did not create — deleting one you merely found is the mistake ownership prevents. Script-created objects are yours to destroy; component-created ones need the component removed; the default world is engine-owned and cannot be removed.

## What you are likely to get wrong

Core's API is young and was renamed — your recall is unreliable. Legacy's page locations were reorganised — your recall of URLs is stale. Nothing carries over between systems unverified.

| Your likely assumption | Actually |
| --- | --- |
| Legacy manual pages are flat, like `class-Rigidbody2D.html` | Reorganised under family folders: `2d-physics/rigidbody-2d/`, `2d-physics/collider-2d/` |
| A Core component holds the state, as Rigidbody 2D does | It owns a body, shape or joint. Operate on the owned object |
| The namespace is `UnityEngine.LowLevelPhysics2D` | 6000.4 and earlier. From 6000.5 it is `Unity.U2D.Physics` |
| Box2D v2 naming and semantics apply to Core | It is Box2D v3. Most older forum answers describe v2 |
| Angles are in radians, as Box2D uses | Read the property page. Hinge angles are documented in degrees |
| Core collision layers are a 32-bit mask | 64 layers in Core, 32 in legacy |
| Some behaviour is component-only | None is |
| A user's own component needs wiring to work with Unity's | None. All interaction is engine-side |
| Any handle can be destroyed | Only what you own. See [Ownership](#ownership) |
| You must create a world first | `PhysicsWorld.defaultWorld` exists in an empty scene |
| Debug visuals need gizmos or `Debug.DrawLine` | Core has a physics renderer with automatic and explicit draw calls. Never hand-roll it |
| A method exists because the other system has one like it | Confirm on its own page |
| `bodyType` and `RigidbodyType2D` set body type | Obsolete. Use `type` with `PhysicsBody.BodyType` |
| A newly created body has a known default type | Do not assume — set `.type` explicitly. falls/thrown/bounces → Dynamic; ground/wall/anchor → Static |
| "add a circle/capsule/polygon/segment area" means the dedicated component | Could be that or Primitive with `shapeType` — same shape, two routes. Prefer dedicated when fixed at authoring time; Primitive only if it must switch at runtime. Chain Segment has no dedicated component |

## Manual: 2D Physics Core

Base: `https://docs.unity.com/en-us/engine/<VERSION>/manual/unity2d/2d-physics-api/`. Pages are `.md`. The landing page is `https://docs.unity.com/en-us/engine/<VERSION>/manual/unity2d/2d-physics-api.md` — contents.

Paths below are relative to the base:
- `introduction.md` — what it is, component-to-object mapping
- `get-started.md` — first scene with components
- `create-objects.md` — Physics Pose and Physics Area components. Children in `create-objects/`: `pose-and-area.md`, `physics-object.md`, `add-sprite.md`, `debug-drawing.md` (Scene view editing, rendering modes)
- `connect-objects.md` — joints. Children in `connect-objects/`: `joints.md`, `create-constraint.md`, `constraint-events.md`
- `properties.md` — definitions, pinned properties, custom data, global settings. Children in `properties/`: `definitions.md`, `pin-properties.md`, `custom-data.md`, `class-physics-core-settings2d.md`, `preferences-window-reference.md`
- `interactions.md` — collisions, contacts, triggers, filtering (queries are `PhysicsWorld` cast/overlap methods in the scripting reference). Children in `interactions/`: `introduction.md`, `collisions-enable.md`, `collision-handle.md`

Read in full before acting (outside this skill's scope): `worlds.md`, `3d-planes.md`, `multithreading.md`.

Component reference pages follow a pattern — build them:
- Body: `create-objects/reference-body.md`
- Area: `create-objects/reference-area-<kind>.md` (capsule, circle, composite, contour, path, polygon, primitive, segment, sprite)
- Joint: `connect-objects/reference-joint-<kind>.md` (distance, fixed, hinge, relative, slider, wheel)
- Constraint: `connect-objects/reference-constraint-ignore.md`
- Simulation: `worlds/reference-simulation-component.md`
- Assets: `worlds/reference-world.md` (Physics Simulation Definition), `worlds/reference-simulation-asset.md` (Physics Simulation World)

This manual covers components. For joint structs, definitions, geometry, queries, math, events and destruction — use the scripting reference.

## Scripting reference

Build the URL — do not search for member pages. Names are lowercase.

Base: `https://docs.unity.com/en-us/engine/<VERSION>/script-reference/`

| You want | Pattern | Example |
| --- | --- | --- |
| Legacy type in `UnityEngine` | `unityengine/<type>.md` | `unityengine/rigidbody2d.md` |
| Core namespace (lists types) | `unity/u2d/physics.md` | |
| Core type | `unity/u2d/physics/<type>.md` | `unity/u2d/physics/physicsbody.md` |
| Core type, 6000.3–6000.4 | `unityengine/lowlevelphysics2d/<type>.md` | `unityengine/lowlevelphysics2d/physicsbody.md` |
| Method, property, field or nested type | `<type page without .md>/<member>.md` | `unity/u2d/physics/physicsbody/createshape.md`, `unity/u2d/physics/physicsbody/linearvelocity.md` |
| A joint in script | Start at `unity/u2d/physics/physicsjoint.md`, then `physics<kind>joint` and `physics<kind>jointdefinition` | `unity/u2d/physics/physicshingejoint.md` |

A type page lists members; a method page lists every overload. Units, ranges and defaults are on property pages. A 404 means the name is wrong, or that version's docs aren't published — fetch the type page and read its members, or try an adjacent version.

For the Core renderer: start at `unity/u2d/physics/physicsworld/renderingmode.md`, `unity/u2d/physics/physicscoresettings2d.md`, and `physicsworld` draw methods: `drawgeometry`, `drawshapeproxy`, `drawqueryresult`, `drawlinestrip`, `drawshapes`.

## Package documentation

Package API pages are live only — not local files. Fetch using the version from `Packages/manifest.json`:
`https://docs.unity3d.com/Packages/com.unity.2d.physics@<major.minor>/api/Unity.U2D.Physics.<Type>.html`

Package manual pages are local: `<Project folder>/Library/PackageCache/com.unity.2d.physics@*/Documentation~/`. Read only `.md` files in that subfolder.

The `api/index.html` listing fetches as nothing — go straight to a named type page.
28 changes: 28 additions & 0 deletions skills/create-2d-physics/references/legacy-2d-physics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Legacy 2D physics

Relative paths in this file resolve to the skill directory — the folder that holds the `SKILL.md` for this skill.

## Step 0

Before you go forward with a workflow or diagnosis:

1. Open and read the [documentation page(s)](#manual-legacy-2d-physics) that cover the specific subject related to what you intend to do or fix and check if you've missed anything.
2. Name the file(s) you read in your response.

This is because your training knowledge about Unity might be out of date, incorrect, or for the wrong Unity version.

## Manual: legacy 2D physics

Base: `https://docs.unity.com/en-us/engine/<VERSION>/manual/unity2d/`. Pages are `.md`.

`2d-physics.md` is the contents page. Landing pages, relative to the base: `2d-physics/rigidbody-2d.md`, `2d-physics/collider-2d.md`, `2d-physics/effectors-2d.md`, `2d-physics/2d-joints.md`, `2d-physics/physics-2d-profiler.md`, plus flat `2d-physics/constant-force-2d-reference.md` and `2d-physics/physics-material-2d-reference.md`.

Each landing page has a folder of the same name listing its children — follow those rather than guessing leaf names.

## Scripting reference

Legacy types live in the main scripting reference as `unityengine/<type>.md`, e.g. `https://docs.unity.com/en-us/engine/<VERSION>/script-reference/unityengine/rigidbody2d.md`. See the [Scripting reference](2d-physics-core.md#scripting-reference) table in the Core reference for the full URL pattern, including how it differs from Core types.

## What you are likely to get wrong

Legacy's page locations were reorganised — your recall of URLs is stale. Legacy manual pages are not flat like `class-Rigidbody2D.html`; they're reorganised under family folders: `2d-physics/rigidbody-2d/`, `2d-physics/collider-2d/`. Core collision layers use a 64-bit mask; legacy uses 32.
Loading
Loading