ForceMode为枚举类型,用来控制力的作用方式:

//
    // 摘要:
    //     Use ForceMode to specify how to apply a force using Rigidbody.AddForce.
    public enum ForceMode
    {
        //
        // 摘要:
        //     Add a continuous force to the rigidbody, using its mass.
        //用其质量给刚体加一个连续的力
        Force = 0,
        //
        // 摘要:
        //     Add an instant force impulse to the rigidbody, using its mass.
        //利用其质量,在刚体上加一个瞬时力冲量。
        Impulse = 1,
        //
        // 摘要:
        //     Add an instant velocity change to the rigidbody, ignoring its mass.
        //在刚体上增加瞬时速度变化量,忽略其质量。
        VelocityChange = 2,
        //
        // 摘要:
        //     Add a continuous acceleration to the rigidbody, ignoring its mass.
        //给刚体增加一个连续的加速度,忽略它的质量
        
        Acceleration = 5
    }

(1)ForceMode.Force:默认方式,使用刚体的质量计算,以每帧间隔时间为单位计算动量。设FixedUpdate()的执行频率采用系统默认值(即0.02s),,则由动量定理

f•t=m•v

可得:100.02=2v1,从而可得v1=0.1,即每帧刚体在X轴上值增加0.1米,从而可计算得刚体的每秒移动速度为v2=(1/0.02)*v1=5m/s。

(2)ForceMode.Acceleration:在此种作用方式下会忽略刚体的实际质量而采用默认值1.0f,时间间隔以系统帧频间隔计算(默认值为0.02s),即

f•t=1.0•v

即可得v1= f•t=10*0.02=0.2,即刚体每帧增加0.2米,从而可得刚体的每秒移动速度为v2=(1/0.02)*v1=10m/s。

(3)ForceMode.Impulse:此种方式采用瞬间力作用方式,即把t的值默认为1,不再采用系统的帧频间隔,即

f•1.0=m•v

即可得v1=f/m=10.0/2.0=5.0,即刚体每帧增加5.0米,从而可得刚体每秒的速度为v2=(1/0.02)*5.0=250m/s。

(4)ForceMode.VelocityChange:此种作用方式下将忽略刚体的实际质量,采用默认质量1.0,同时也忽略系统的实际帧频间隔,采用默认间隔1.0,即

f•1.0=1.0•v

即可得v1=f=10.0,即刚体每帧沿X轴移动距离为10米,从而可得刚体每秒的速度为v2=(1/0.02)*v1=500m/s。

实例演示:下面通过实例演示作用力方式ForceMode中各种作用力类型的使用。

using UnityEngine;
using System.Collections;
 
public class ForceMode_ts : MonoBehaviour
{
    public Rigidbody A, B, C, D;
    //作用力向量
    Vector3 forces = new Vector3(10.0f, 0.0f, 0.0f);
 
    void Start()
    {
        //初始化4个刚体的质量,使其相同
        A.mass = 2.0f;
        B.mass = 2.0f;
        C.mass = 2.0f;
        D.mass = 2.0f;
        //对A、B、C、D采用不同的作用力方式
        //注意此处只是对物体增加了1帧的作用力
        //如果要对刚体产生持续作用力请把以下代码放在FixedUpdate()方法中
        A.AddForce(forces, ForceMode.Force);
        B.AddForce(forces, ForceMode.Acceleration);
        C.AddForce(forces, ForceMode.Impulse);
        D.AddForce(forces, ForceMode.VelocityChange);
    }
 
    void FixedUpdate()
    {
        Debug.Log("ForceMode.Force作用方式下A每帧增加的速度:" + A.velocity);
        Debug.Log("ForceMode.Acceleration作用方式下B每帧增加的速度:" + B.velocity);
        Debug.Log("ForceMode.Impulse作用方式下C每帧增加的速度:" + C.velocity);
        Debug.Log("ForceMode.VelocityChange作用方式下D每帧增加的速度:" + D.velocity);
    }
}