create-2d-physics
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreate and fix Unity 2D physics
创建与修复Unity 2D物理
Do not weigh 3D physics options. If evidence shows the user means 3D, say so and stop.
Prefer over — faster and lands on the exact reference. Only fetch what you need. Replace with the following before fetching:
WebFetchWebSearch<VERSION>- docs.unity.com: The Unity version, e.g. . Pages are markdown — URLs end in
6000.7..md - Package on docs.unity3d.com: The package version, e.g. . Check
@1.1if unsure.Packages/manifest.json
不要考虑3D物理相关选项。如果有证据表明用户指的是3D物理,请说明情况并停止操作。
优先使用而非——速度更快且能直接定位到准确参考文档。仅获取你需要的内容。在获取前请将替换为以下对应内容:
WebFetchWebSearch<VERSION>- docs.unity.com:Unity版本号,例如。页面为Markdown格式——URL以
6000.7结尾。.md - docs.unity3d.com上的包文档:包版本号,例如。如果不确定请查看
@1.1。Packages/manifest.json
Important
重要说明
- Use the unity cli () and the
unity command --format jsonandunity-cliskills for each step.unity-pipeline - Don't use directives or unqualified types in the
usingforsnippet. The compiler reads usingevaland a bare AssetDatabase, Volume, or Object causes errors (CS0246 / CS0103 / CS0104).UnityEnginewhat you want to read; it arrives atreturn.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.
- Never reflect over types to discover an API — fetch the member page instead. The system probe is the one exception.
- 每一步都要使用unity cli()以及
unity command --format json和unity-cli技能。unity-pipeline - 不要在的
eval中使用snippet指令或未限定的类型。编译器会默认引用using,直接写AssetDatabase、Volume或Object会导致错误(CS0246 / CS0103 / CS0104)。用UnityEngine返回你需要读取的内容;结果会存放在return中。data.result.result - 创建还原点,以便在修改失败时可以回滚。
- 仅执行要求的操作,不要修改不相关的资源或文件。
- 避免冗长的解释。
- 绝对不要在用户的脚本中留下验证代码;不要添加用户未要求的日志或沉降检测功能。参见最后一步。
- 不要通过反射类型来查找API——请改为获取对应成员的文档页面。系统探测是唯一的例外。
Step 0: Which physics system
步骤0:判断使用哪套物理系统
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 |
| Core in script |
| 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 and Unity 6000.7+.
com.unity.2d.physics - Core in script = API, no package needed.
Unity.U2D.Physics
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 , Core as , Core components present. Core exists if either Core value is true; the true one gives the namespace. rules that route out.
Unity.U2D.PhysicsUnityEngine.LowLevelPhysics2DfalseUnity有两套2D物理系统,它们没有共享代码,也不会相互交互。在编写任何物理代码前请先确定使用哪一套——用错系统的代码虽然能编译运行,但不会产生任何效果。
请对照下表检查用户请求、场景和现有脚本。组件是可靠的判断依据——请先检查场景和现有脚本。同时也要参考用户请求的措辞:例如"add a collider"属于组件操作相关表述,"spawn a thousand"属于脚本操作相关表述。
| 判断依据 | 对应系统 |
|---|---|
| Physics Pose、Area、Constraint或Simulation组件 | Core组件,需6000.7+版本及对应包 |
| Rigidbody 2D、Collider 2D、Joint 2D、Effector 2D | 旧版(Legacy) |
脚本中使用 | 脚本式Core |
使用 | 旧版(Legacy) |
| 两者都存在 | 独立模拟。请说明该情况,分别处理 |
| 无判断依据 | 停止并询问,提供上述三种可选方案 |
说明:
- 旧版(Legacy)= Rigidbody 2D和Collider 2D组件。
- Core组件 = Physics Pose和Physics Area,需要安装包且Unity版本为6000.7+。
com.unity.2d.physics - 脚本式Core = API,无需安装包。
Unity.U2D.Physics
如果仍然不确定安装了哪些系统,请运行探测命令。如果还是无法确定使用哪套方案,请停止并询问用户——提供所有三种可选方案,不要替用户做选择:
探测命令:
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;'探测命令返回结果依次为:旧版是否存在、版Core是否存在、版Core是否存在、Core组件是否存在。只要任意一个Core相关值为true即表示Core系统存在;为true的那个对应的就是使用的命名空间。值为则表示该方案不可用。
Unity.U2D.PhysicsUnityEngine.LowLevelPhysics2DfalseSystem reference
系统参考文档
Once you know the route, read the matching reference before acting:
- 2D Physics Core reference - components, configuring them, creating bodies in script, ownership, and the full Core topic map.
- Legacy 2D physics reference - the legacy manual topic map.
确定使用的方案后,请先阅读对应的参考文档再开始操作:
- 2D Physics Core参考文档 —— 包含组件说明、配置方法、脚本创建刚体、所有权以及完整的Core主题索引。
- 旧版2D物理参考文档 —— 旧版物理手册的主题索引。
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 . Check all three before inventing a pattern.
https://github.com/Unity-Technologies/PhysicsExamples2D优先按以下顺序参考示例:你正在使用的类型或成员页面上的C#示例;项目中已有的脚本(匹配其代码风格); 上的示例项目。在自行设计实现模式前,请先检查这三个来源。
https://github.com/Unity-Technologies/PhysicsExamples2DFinal step
最后一步
Do the following checks:
- Check the project has zero console errors. Use for counts.
console_status - Stop there unless the user asked you to prove it works or you suspect a specific fault.
- If you do enter Play mode, do it once only to chase a fault you already suspect. Read the world through rather than adding logging. Take no screenshots as they take too much time.
eval
If a check fails, go back and reread the docs pages in the matching reference to find out what you missed.
请执行以下检查:
- 检查项目没有控制台错误。使用统计错误数量。
console_status - 到此为止,除非用户要求验证功能正常,或者你怀疑存在特定问题。
- 如果确实需要进入Play mode,仅进入一次来排查你已经怀疑的问题。通过读取场景状态,不要添加日志。不要截图,因为太耗费时间。
eval
如果检查未通过,请返回并重新阅读对应参考文档中的页面,找出你遗漏的内容。
Final report
最终报告
Short checklist: what you changed and why, which physics system you used, anything left for the user to decide or do.
简短的检查清单:你修改了什么、修改原因、使用了哪套物理系统、还有哪些需要用户决定或完成的事项。