通过物理模拟控制对象的位置。
向对象添加 Rigidbody 组件后,其运动将受到 Unity 物理引擎的控制。即使不添加任何代码,Rigidbody 对象也受到向下的重力,并在与其他对象碰撞时作出反应(如果也存在适当的 Collider 组件)。
Rigidbody 还有一个脚本 API,让您能够向对象施加力,并以逼真的物理效果对其进行控制。例如,可以根据车轮施加的力来指定汽车的行为。根据这些信息,物理引擎可以处理汽车运动的大多数其他方面,因此汽车可进行逼真的加速并适当地响应碰撞。
在脚本中,建议使用 FixedUpdate 函数来施加力和更改 Rigidbody 设置(而不是使用 Update,Update 用于大多数其他帧更新任务)。这样做的原因是物理更新在测量的时间步骤中执行,而时间步骤与帧更新不一致。FixedUpdate 在每次进行物理更新前调用,因此在该函数中做出的任何更改都将直接处理。
刚开始使用 Rigidbody 时,新手常常会遇到游戏物理效果似乎以“慢动作”运行的问题。这实际上是您的模型使用了不适当的缩放导致的。默认的重力设置假定一个世界单位对应于一米的距离。对于非物理游戏,如果您的模型长度都为 100 个单位,则不会有太大的差别;但在使用物理引擎时,它们将被视为非常大的对象。如果为本应较小的对象使用较大的缩放,它们下落时的视觉速度就会相当缓慢 - 物理引擎认为它们是非常大的物体,正在很远的距离处下落。因此,请确保您的对象缩放后与现实世界的对象差不多(例如,汽车的长度应为 4 个单位 = 4 米左右)。
angularDrag | 对象的角阻力。 |
angularVelocity | 刚体的角速度矢量(以弧度/秒为单位)。 |
centerOfMass | 相对于变换原点的质心。 |
collisionDetectionMode | 刚体的碰撞检测模式。 |
constraints | 控制该刚体的模拟自由度。 |
detectCollisions | 是否应启用碰撞检测?(默认情况下始终启用)。 |
drag | 对象的阻力。 |
freezeRotation | 控制物理是否会更改对象的旋转。 |
inertiaTensor | 质量相对于质心的对角惯性张量。 |
inertiaTensorRotation | 惯性张量的旋转。 |
interpolation | 插值可以平滑消除固定帧率运行物理导致的现象。 |
isKinematic | 控制物理是否影响刚体。 |
mass | 刚体的质量。 |
maxAngularVelocity | 刚体的最大角速率(以弧度/秒为单位)。(默认值为 7)范围为 { 0, 无穷大 }。 |
maxDepenetrationVelocity | 离开穿透状态时刚体的最大速度。 |
position | 刚体的位置。 |
rotation | 刚体的旋转。 |
sleepThreshold | 经过质量标准化的能量阈值 - 当低于该阈值时,对象开始进入睡眠状态。 |
solverIterations | solverIterations 确定刚体关节与碰撞接触点的解析精确度。它将重写 Physics.defaultSolverIterations。必须为正值。 |
solverVelocityIterations | solverVelocityIterations 影响刚体关节与碰撞接触点的解析精确度。它将重写 Physics.defaultSolverVelocityIterations。必须为正值。 |
useGravity | 控制重力是否影响该刚体。 |
velocity | 刚体的速度矢量。它表示刚体位置的变化率。 |
worldCenterOfMass | 刚体在世界空间中的质心(只读)。 |
AddExplosionForce | 向模拟爆炸效果的刚体施加力。 |
AddForce | 向 Rigidbody 添加力。 |
AddForceAtPosition | 在 position 处施加 /force/。这将向对象施加扭矩和力。 |
AddRelativeForce | 向刚体添加力(相对于其坐标系)。 |
AddRelativeTorque | 向刚体添加扭矩(相对于其坐标系)。 |
AddTorque | 向刚体添加扭矩。 |
ClosestPointOnBounds | 与附加碰撞体的包围盒最接近的点。 |
GetPointVelocity | 点 /worldPoint/(全局空间)处刚体的速度。 |
GetRelativePointVelocity | 相对于点 relativePoint 处的刚体的速度。 |
IsSleeping | 刚体是否处于睡眠状态? |
MovePosition | 将运动 Rigidbody 向 position 移动。 |
MoveRotation | 将刚体旋转到 /rotation/。 |
ResetCenterOfMass | 重置刚体的质心。 |
ResetInertiaTensor | 重置惯性张量的值和旋转。 |
SetDensity | 根据附加的碰撞体设置质量(假设密度恒定)。 |
Sleep | 强制刚体进入睡眠状态至少一帧。 |
SweepTest | 测试如果刚体在场景中移动时,是否会与任何对象发生碰撞。 |
SweepTestAll | 与 Rigidbody.SweepTest 类似,但返回所有命中对象。 |
WakeUp | 强制唤醒刚体。 |
OnCollisionEnter | 当该碰撞体/刚体已开始接触另一个刚体/碰撞体时,调用 OnCollisionEnter。 |
OnCollisionExit | 当该碰撞体/刚体已停止接触另一个刚体/碰撞体时,调用 OnCollisionExit。 |
OnCollisionStay | 对应正在接触刚体/碰撞体的每一个碰撞体/刚体,每帧调用一次 OnCollisionStay。 |
gameObject | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
BroadcastMessage | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 |
CompareTag | 此游戏对象是否使用 tag 进行了标记? |
GetComponent | Returns the component of Type type if the GameObject has one attached, null if it doesn't. Will also return disabled components. |
GetComponentInChildren | 使用深度首次搜索返回 GameObject 或其任何子项中类型为 type 的组件。 |
GetComponentInParent | 返回 GameObject 或其任何父项中类型为 type 的组件。 |
GetComponents | 返回 GameObject 中类型为 type 的所有组件。 |
GetComponentsInChildren | Returns all components of Type type in the GameObject or any of its children. Works recursively. |
GetComponentsInParent | 返回 GameObject 或其任何父项中类型为 type 的所有组件。 |
SendMessage | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 |
SendMessageUpwards | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 |
TryGetComponent | 获取指定类型的组件(如果存在)。 |
GetInstanceID | 返回对象的实例 ID。 |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindObjectOfType | 返回第一个类型为 type 的已加载的激活对象。 |
FindObjectsOfType | Gets a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.