create-2d-physics

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Create 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
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.
不要考虑3D物理相关选项。如果有证据表明用户指的是3D物理,请说明情况并停止操作。
优先使用
WebFetch
而非
WebSearch
——速度更快且能直接定位到准确参考文档。仅获取你需要的内容。在获取前请将
<VERSION>
替换为以下对应内容:
  • docs.unity.com:Unity版本号,例如
    6000.7
    。页面为Markdown格式——URL以
    .md
    结尾。
  • docs.unity3d.com上的包文档:包版本号,例如
    @1.1
    。如果不确定请查看
    Packages/manifest.json
    。

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.
  • 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
    指令或未限定的类型。编译器会默认引用
    UnityEngine
    ,直接写AssetDatabase、Volume或Object会导致错误(CS0246 / CS0103 / CS0104)。用
    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.
SignalSystem
Physics Pose, Area, Constraint or Simulation componentsCore components, 6000.7+ with the package
Rigidbody 2D, Collider 2D, Joint 2D, Effector 2DLegacy
Unity.U2D.Physics
or
UnityEngine.LowLevelPhysics2D
types in script
Core in script
Rigidbody2D
,
Collider2D
,
Physics2D
types
Legacy
Both presentSeparate simulations. Say so, treat separately
No signalStop 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.
Unity有两套2D物理系统,它们没有共享代码,也不会相互交互。在编写任何物理代码前请先确定使用哪一套——用错系统的代码虽然能编译运行,但不会产生任何效果。
请对照下表检查用户请求、场景和现有脚本。组件是可靠的判断依据——请先检查场景和现有脚本。同时也要参考用户请求的措辞:例如"add a collider"属于组件操作相关表述,"spawn a thousand"属于脚本操作相关表述。
判断依据对应系统
Physics Pose、Area、Constraint或Simulation组件Core组件,需6000.7+版本及对应包
Rigidbody 2D、Collider 2D、Joint 2D、Effector 2D旧版(Legacy)
脚本中使用
Unity.U2D.Physics
或
UnityEngine.LowLevelPhysics2D
类型
脚本式Core
使用
Rigidbody2D
、
Collider2D
、
Physics2D
类型
旧版(Legacy)
两者都存在独立模拟。请说明该情况,分别处理
无判断依据停止并询问,提供上述三种可选方案
说明:
  • 旧版(Legacy)= Rigidbody 2D和Collider 2D组件。
  • Core组件 = Physics Pose和Physics Area,需要安装
    com.unity.2d.physics
    包且Unity版本为6000.7+。
  • 脚本式Core =
    Unity.U2D.Physics
    API,无需安装包。
如果仍然不确定安装了哪些系统,请运行探测命令。如果还是无法确定使用哪套方案,请停止并询问用户——提供所有三种可选方案,不要替用户做选择:
探测命令:
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;'
探测命令返回结果依次为:旧版是否存在、
Unity.U2D.Physics
版Core是否存在、
UnityEngine.LowLevelPhysics2D
版Core是否存在、Core组件是否存在。只要任意一个Core相关值为true即表示Core系统存在;为true的那个对应的就是使用的命名空间。值为
false
则表示该方案不可用。

System 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
https://github.com/Unity-Technologies/PhysicsExamples2D
. Check all three before inventing a pattern.
优先按以下顺序参考示例:你正在使用的类型或成员页面上的C#示例;项目中已有的脚本(匹配其代码风格);
https://github.com/Unity-Technologies/PhysicsExamples2D
上的示例项目。在自行设计实现模式前,请先检查这三个来源。

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.
请执行以下检查:
  1. 检查项目没有控制台错误。使用
    console_status
    统计错误数量。
  2. 到此为止,除非用户要求验证功能正常,或者你怀疑存在特定问题。
  3. 如果确实需要进入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.
简短的检查清单:你修改了什么、修改原因、使用了哪套物理系统、还有哪些需要用户决定或完成的事项。