通过物理模拟控制对象的位置。
向对象添加 Rigidbody 组件后,其运动将受到 Unity 物理引擎的控制。即使不添加任何代码,Rigidbody 对象也受到向下的重力,并在与其他对象碰撞时作出反应(如果也存在适当的 Collider 组件)。
Rigidbody 还有一个脚本 API,让您能够向对象施加力,并以逼真的物理效果对其进行控制。例如,可以根据车轮施加的力来指定汽车的行为。根据这些信息,物理引擎可以处理汽车运动的大多数其他方面,因此汽车可进行逼真的加速并适当地响应碰撞。
在脚本中,建议使用 FixedUpdate 函数来施加力和更改 Rigidbody 设置(而不是使用 Update,Update 用于大多数其他帧更新任务)。这样做的原因是物理更新在测量的时间步骤中执行,而时间步骤与帧更新不一致。FixedUpdate 在每次进行物理更新前调用,因此在该函数中做出的任何更改都将直接处理。
刚开始使用 Rigidbody 时,新手常常会遇到游戏物理效果似乎以“慢动作”运行的问题。这实际上是您的模型使用了不适当的缩放导致的。默认的重力设置假定一个世界单位对应于一米的距离。对于非物理游戏,如果您的模型长度都为 100 个单位,则不会有太大的差别;但在使用物理引擎时,它们将被视为非常大的对象。如果为本应较小的对象使用较大的缩放,它们下落时的视觉速度就会相当缓慢 - 物理引擎认为它们是非常大的物体,正在很远的距离处下落。因此,请确保您的对象缩放后与现实世界的对象差不多(例如,汽车的长度应为 4 个单位 = 4 米左右)。
angularDrag | 对象的角阻力。 |
angularVelocity | 刚体的角速度矢量(以弧度/秒为单位)。 |
automaticCenterOfMass | Whether or not to calculate the center of mass automatically. |
automaticInertiaTensor | Whether or not to calculate the inertia tensor automatically. |
centerOfMass | 相对于变换原点的质心。 |
collisionDetectionMode | 刚体的碰撞检测模式。 |
constraints | 控制该刚体的模拟自由度。 |
detectCollisions | 是否应启用碰撞检测?(默认情况下始终启用)。 |
drag | 对象的阻力。 |
excludeLayers | The additional layers that all Colliders attached to this Rigidbody should exclude when deciding if the Collider can come into contact with another Collider. |
freezeRotation | 控制物理是否会更改对象的旋转。 |
includeLayers | The additional layers that all Colliders attached to this Rigidbody should include when deciding if the Collider can come into contact with another Collider. |
inertiaTensor | The inertia tensor of this body, defined as a diagonal matrix in a reference frame positioned at this body's center of mass and rotated by Rigidbody.inertiaTensorRotation. |
inertiaTensorRotation | 惯性张量的旋转。 |
interpolation | Interpolation provides a way to manage the appearance of jitter in the movement of your Rigidbody GameObjects at run time. |
isKinematic | 控制物理是否影响刚体。 |
mass | 刚体的质量。 |
maxAngularVelocity | The maximum angular velocity of the rigidbody measured in radians per second. (Default 7) range { 0, infinity }. |
maxDepenetrationVelocity | 离开穿透状态时刚体的最大速度。 |
maxLinearVelocity | The maximum linear velocity of the rigidbody measured in meters per second. |
position | 刚体的位置。 |
rotation | 刚体的旋转。 |
sleepThreshold | 经过质量标准化的能量阈值 - 当低于该阈值时,对象开始进入睡眠状态。 |
solverIterations | solverIterations 确定刚体关节与碰撞接触点的解析精确度。它将重写 Physics.defaultSolverIterations。必须为正值。 |
solverVelocityIterations | solverVelocityIterations 影响刚体关节与碰撞接触点的解析精确度。它将重写 Physics.defaultSolverVelocityIterations。必须为正值。 |
useGravity | 控制重力是否影响该刚体。 |
velocity | 刚体的速度矢量。它表示刚体位置的变化率。 |
worldCenterOfMass | 刚体在世界空间中的质心(只读)。 |
AddExplosionForce | 向模拟爆炸效果的刚体施加力。 |
AddForce | 向 Rigidbody 添加力。 |
AddForceAtPosition | 在 position 处施加 /force/。这将向对象施加扭矩和力。 |
AddRelativeForce | 向刚体添加力(相对于其坐标系)。 |
AddRelativeTorque | 向刚体添加扭矩(相对于其坐标系)。 |
AddTorque | 向刚体添加扭矩。 |
ClosestPointOnBounds | 与附加碰撞体的包围盒最接近的点。 |
GetAccumulatedForce | Returns the force that the Rigidbody has accumulated before the simulation step. |
GetAccumulatedTorque | Returns the torque that the Rigidbody has accumulated before the simulation step. |
GetPointVelocity | 点 /worldPoint/(全局空间)处刚体的速度。 |
GetRelativePointVelocity | 相对于点 relativePoint 处的刚体的速度。 |
IsSleeping | 刚体是否处于睡眠状态? |
Move | Moves the Rigidbody to position and rotates the Rigidbody to rotation. |
MovePosition | 将运动 Rigidbody 向 position 移动。 |
MoveRotation | 将刚体旋转到 /rotation/。 |
PublishTransform | Applies the position and rotation of the Rigidbody to the corresponding Transform component. |
ResetCenterOfMass | 重置刚体的质心。 |
ResetInertiaTensor | 重置惯性张量的值和旋转。 |
SetDensity | 根据附加的碰撞体设置质量(假设密度恒定)。 |
Sleep | 强制刚体进入睡眠状态至少一帧。 |
SweepTest | 测试如果刚体在场景中移动时,是否会与任何对象发生碰撞。 |
SweepTestAll | 与 Rigidbody.SweepTest 类似,但返回所有命中对象。 |
WakeUp | 强制唤醒刚体。 |
OnCollisionEnter | 当该碰撞体/刚体已开始接触另一个刚体/碰撞体时,调用 OnCollisionEnter。 |
OnCollisionExit | 当该碰撞体/刚体已停止接触另一个刚体/碰撞体时,调用 OnCollisionExit。 |
OnCollisionStay | OnCollisionStay is called once per frame for every Collider or Rigidbody that touches another Collider or Rigidbody. |
gameObject | 此组件附加到的游戏对象。始终将组件附加到游戏对象。 |
tag | 此游戏对象的标签。 |
transform | 附加到此 GameObject 的 Transform。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
BroadcastMessage | 调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。 |
CompareTag | Checks the GameObject's tag against the defined tag. |
GetComponent | Gets a reference to a component of type T on the same GameObject as the component specified. |
GetComponentInChildren | Gets a reference to a component of type T on the same GameObject as the component specified, or any child of the GameObject. |
GetComponentInParent | Gets a reference to a component of type T on the same GameObject as the component specified, or any parent of the GameObject. |
GetComponents | Gets references to all components of type T on the same GameObject as the component specified. |
GetComponentsInChildren | Gets references to all components of type T on the same GameObject as the component specified, and any child of the GameObject. |
GetComponentsInParent | Gets references to all components of type T on the same GameObject as the component specified, and any parent of the GameObject. |
SendMessage | 调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。 |
SendMessageUpwards | 调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。 |
TryGetComponent | 获取指定类型的组件(如果存在)。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectsByType | Retrieves 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.