Article directory
Click to visit the official website Physics System , Rigidbody Overview , Rigidbody (Rigidbody)
1. Rigid body components
Rigidbodies make the behavior of game objects controlled by physics. Rigidbodies can accept forces and torques, allowing objects to move in a realistic manner. Any game object must contain a rigid body that is affected by gravity, behaves based on applied forces (via scripting), or interacts with other objects via the NVIDIA PhysX physics engine.
Attributes | Function |
---|---|
Mass | The mass of the object (kg by default). |
Drag | Affects the amount of air resistance the object has when moving it according to the force. 0 means no air resistance, and infinity makes the object stop moving immediately. |
Angular Drag | Affects the amount of air resistance the object has when rotating it according to the torque. 0 means no air resistance. Note that directly setting the object's Angular Drag property to infinity will not stop the object from spinning. |
Use Gravity | If this property is enabled, the object will be affected by gravity. |
Is Kinematic | If this option is enabled, the object will not be driven by the physics engine, it can only be manipulated by __Transform__. This property is useful for mobile platforms, or if you want to animate a Rigidbody with a HingeJoint attached to it. |
Interpolate | Only try one of the provided options if you see jerks in rigid body motion. |
● None | No interpolation is applied. |
● Interpolate | Smoothes the transition based on the previous frame's transition. |
● Extrapolate | Smooth the transform based on the estimated transform for the next frame. |
Collision Detection | Useful to prevent fast-moving objects from passing through other objects without detecting collisions. |
● Discrete | Use discrete collision detection for all other colliders in the scene. Other colliders use discrete collision detection when testing for collisions. For normal collisions (this is the default). |
● Continuous | Use discrete collision detection for dynamic colliders (with rigid bodies) and sweep-based continuous collision detection for static colliders (without rigid bodies). Rigidbodies set to __Continuous Dynamic__ will use continuous collision detection when testing for collisions with the rigidbody. Other rigidbodies will use discrete collision detection. Used for __Continuous Dynamic__ to detect objects that need to collide. (This property has a big impact on physics performance, if you don't have collision issues with fast objects, leave it set to Discrete ) |
● Continuous Dynamic | Use continuous sweep-based collision detection for GameObjects with __Continuous__ and __Continuous Dynamic__ collisions set. Continuous collision detection will also be used for static colliders (without rigidbodies). For all other colliders, discrete collision detection is used. For fast moving objects. |
● Continuous Speculative | Use speculative continuous collision detection for rigidbodies and colliders. This is also the only CCD mode where moving objects can be set. This method is generally less expensive than sweep-based continuous collision detection. |
Constraints | Restrictions on rigid body motion: |
● Freeze Position | Optionally stops movement of the rigidbody along the world X, Y, and Z axes. |
● Freeze Rotation | Optionally stops the rigidbody from rotating around the local X, Y, and Z axes. |
Info | Display rigid body information. |
2. Rigid body script
Click to visit the official website Rigidbody Class
2.1. Obtain rigid body
Rigidbody rb;
void Start()
{
rb = transform.GetComponent<Rigidbody>();
}
2.2. Adding force
- Add force to rigidbody (at center):
AddForce(方向大小,力类型)
/**
* enum ForceMode
* Force:使用刚体的质量向刚体添加连续力 F = m · a
* Acceleration:将连续加速度添加到刚体,忽略其质量 v = a · t
* Impulse:使用刚体的质量向刚体添加即时力脉冲 I = F · t = m · a · t = m · v
* VelocityChange:将即时速度变化添加到刚体,忽略其质量
*/
rb.AddForce(new Vector3(0, 0, 1), ForceMode.VelocityChange); // 共 4 个重载
// 上面那句力模型为 VelocityChange 时与下面这句对速度直接赋值效果完全一毛一样
rb.velocity = new Vector3(0, 0, 1);
- Apply force at position. This will apply torque and force to the object:
AddForceAtPosition(方向大小,施加力的位置,力类型)
// 共 2 个重载
rb.AddForceAtPosition(new Vector3(0, 1, 0), forcePosition.position, ForceMode.Impulse);
- Apply a force to a rigidbody that simulates an explosion:
AddExplosionForce(爆炸力,爆炸中心,爆炸半径,...)
// 共3个重载
rb.AddExplosionForce(100, forcePosition.position, 3);
2.3、Move
- Move the rigid body to position:
MovePosition(position)
rb.MovePosition(target.position);
- Rotate the rigidbody to rotation:
MoveRotation(rotation)
rb.MoveRotation(target.rotation);