自上次物理模拟步骤以来,显式应用于此 Rigidbody2D 的力的总量。
当使用 Rigidbody2D.AddForce、Rigidbody2D.AddForceAtPosition 或 Rigidbody2D.AddRelativeForce 向 Rigidbody2D 添加力时,力的总和将被累加。这仅适用于使用 ForceMode2D.Force 时,不适用于使用 ForceMode2D.Impulse 时。
在下一个模拟步骤中,合力将被时间积分到 Rigidbody2D.linearVelocity 中,然后自动重置为 零。
注意:只有具有 动态刚体类型 的 Rigidbody2D 才会响应力和扭矩。在 运动学刚体类型 或 静态刚体类型 上设置此属性将无效。
using UnityEngine; using UnityEngine.Assertions;
public class Example : MonoBehaviour { void Start() { // Fetch the rigidbody. var body = GetComponent<Rigidbody2D>();
// Make the assumption the body has no previous force applied. Assert.AreEqual(Vector2.zero, body.totalForce);
// Initialize a force. var force = new Vector2(3f, 2f);
// Add the force. body.AddForce(force);
// The total force should be what we just added. Assert.AreEqual(force, body.totalForce);
// Add the same force again. body.AddForce(force);
// The total force should still be what we've added. Assert.AreEqual(force * 2f, body.totalForce);
// We can reset any force that has been applied since the last simulation step. body.totalForce = Vector2.zero; } }