unity-physics
它能做什么
该技能讲解如何利用内置物理引擎,让 Unity 中的 3D 对象移动、碰撞并互相检测。适合构建带有物理对象的游戏的开发者。
适用场景
- ✓当你需要 Unity 中的对象受力、重力或速度而移动时。
- ✓当你希望对象碰撞或进入触发区域时,代码能做出反应时。
- ✓当你希望只有特定对象之间才发生碰撞,或需要用射线检查地面或视线时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unity/unity-physics/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请让我的球在 Unity 中沿斜坡滚下,并在撞到墙时停下。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解 Rigidbody 与碰撞体组件如何协同工作,如何在固定步长函数中更新物理,触发器与碰撞有何不同,以及图层如何决定哪些对象可以碰撞。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Unity Physics (Rigidbody / PhysX)
Make objects move, collide, and detect each other with Unity 6.3 LTS's built-in 3D physics
(PhysX). Get the FixedUpdate discipline, trigger-vs-collision rules, and collision layers
right. Targets Unity 6.3 LTS (6000.3).
Unity 6 renames:
Rigidbody.velocity→linearVelocity,drag→linearDamping,angularDrag→angularDamping. The old names still compile in 6.3 LTS but are obsolete (warnings; the API Updater offers the rename), so write the new names.PhysicMaterial→PhysicsMaterialis a hard compile error in old code.
When to use
- Use when giving an object physical motion (forces, velocity, gravity), responding to collisions or triggers, setting up collision layers/masks, raycasting for ground checks or line-of-sight, or connecting bodies with joints.
- Use when scenes/prefabs contain
Rigidbody+Collidercomponents.
When not to use: 2D physics (Rigidbody2D, Collider2D) is a separate API — adapt the
concepts but the types differ. Unity 6.3 also has a separate low-level 2D API
(UnityEngine.LowLevelPhysics2D: PhysicsWorld, PhysicsBody) with its own isolated worlds;
it is not the Rigidbody2D system. Cross-engine feel tuning (timestep, jitter, tunnelling) →
physics-tuning. Reading input that drives movement → unity-input-system.
Core workflow
- Add a
Rigidbodyto anything that should be simulated; add aColliderto anything that should be hit.OnCollision*messages need a Collider on both and at least one dynamic (non-kinematic) Rigidbody: with the default Contact Pairs Mode, kinematic vs kinematic and kinematic vs static send no collision messages. - Do all physics in
FixedUpdate. Read input inUpdate, store intent, then apply forces / setlinearVelocity/ callMovePositioninFixedUpdate. - Move bodies through the physics API, not the Transform. Use
AddForce,linearVelocity, orMovePosition— never assigntransform.positionto a non-kinematic Rigidbody (it teleports and breaks collision resolution). - Pick collision vs trigger. A solid collision blocks and calls
OnCollisionEnter; aColliderwithIs Triggerchecked passes through and callsOnTriggerEnter. - Organise interactions with layers. Put objects on layers and edit the Layer Collision Matrix (Project Settings → Physics) so unrelated things don't test against each other.
- Verify with the Physics Debugger (Window → Analysis → Physics Debugger) and by watching for jitter; if fast objects pass through walls, raise Collision Detection mode.
Patterns
1. Force-based movement in FixedUpdate (with a speed clamp)
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Mover : MonoBehaviour
{
[SerializeField] private float accel = 30f, maxSpeed = 8f;
private Rigidbody _rb;
private Vector3 _input; // set from Update / input system
private void Awake() => _rb = GetComponent<Rigidbody>();
private void FixedUpdate()
{
_rb.AddForce(_input * accel, ForceMode.Acceleration); // mass-independent accel
// Unity 6.3 LTS: linearVelocity (was 'velocity'). Clamp horizontal speed.
Vector3 flat = new(_rb.linearVelocity.x, 0, _rb.linearVelocity.z);
if (flat.magnitude > maxSpeed)
{
flat = flat.normalized * maxSpeed;
_rb.linearVelocity = new Vector3(flat.x, _rb.linearVelocity.y, flat.z);
}
}
}
ForceMode: Force (continuous, mass-scaled), Acceleration (continuous, ignores mass),
Impulse (instant, mass-scaled — jumps), VelocityChange (instant, ignores mass).
2. Collision vs trigger callbacks
// Solid hit: both have colliders, at least one has a dynamic (non-kinematic) Rigidbody.
private void OnCollisionEnter(Collision col)
{
// GetContact avoids the garbage that the col.contacts array allocates.
Debug.Log($"Hit {col.gameObject.name} at {col.GetContact(0).point}");
}
// Overlap: one collider has 'Is Trigger' = true, and at least one party has a Rigidbody
// (or a Rigidbody carries the trigger). Two static colliders never report.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Pickup")) Destroy(other.gameObject);
}
3. Ground check with a layer-masked raycast
[SerializeField] private LayerMask groundMask; // set to your "Ground" layer in the Inspector
常见问题
什么是 Rigidbody?
Rigidbody 是一个让对象遵循物理规律的组件,例如重力和碰撞。
触发器和碰撞有什么区别?
触发器会检测对象何时进入,但不会阻挡它们;而碰撞会实际阻止它们。
它涵盖 2D 物理吗?
不涵盖。2D 物理使用不同的组件,因此不在本技能范围之内。
详情
作者的原始描述
Set up 3D physics in Unity 6.3 LTS: Rigidbody movement and forces, colliders, triggers vs collisions, layer-based collision, raycasts, and joints. Use when adding a Rigidbody, handling OnCollisionEnter/OnTriggerEnter, tuning collision layers, casting rays, or when the user mentions Unity physics, AddForce, isKinematic, or linearVelocity.
热度
- GitHub 星标
- 6.6k
- 排名
- #13849
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …