[Unity][Help Documentation] Detailed explanation of AddForce function, selection of parameters ForceMode (Acceleration, Force, Impulse and VelocityChange)

background

I often forget, check often, it is better for me to write an article for my own reference, after all, every time I check on a certain N station, it is a machine-translated article that I don’t know which year I copied from each other.

This article involves code and test reference unity version 2021.3,
insert image description here

AddForce

Used to add force to the rigidbody component object.
Its parameters determine how the added force works, so the effects (momentum, kinetic energy) obtained by different parameters are also different.
In addition, the default acceleration of gravity in unity is about 9.81, which will change with the condition of the object. Use Debug.Log("Gravity: " + Physics.gravity);to view the acceleration of gravity (the acceleration of gravity also has a direction! So is also a Vector3).

main point

You need to know and understand the following

  • The force is a vector and has a direction, so the first parameter when calling is a Vector3 type

  • No matter what parameter is chosen to apply the force, the force is not automatically applied to subsequent frames. It will only be applied the moment you call the AddForce function. The difference is only how it calculates how much force to apply, not how it applies it. https://answers.unity.com/questions/696068/difference-between-forcemodeforceaccelerationimpul.html

  • When using AddForce, you don't need to manually use Time.deltaTime or
    fixedDeltaTime, its internal physics function will automatically take time into calculation.
    https://answers.unity.com/questions/1752181/does-addforce-with-forcemodeforce-use-timedeltatim.html

  • The physics-related code is best performed in the fixUpdate function instead of update, because the time interval of fixupdate is fixed, and the time interval of update depends on the frame rate, and it is uncertain every time.

  • The continuous force (continuous call in fixUpdate) uses the Force and Acceleration parameters, the single force uses the Impulse parameter, and the velocity change uses the VelocityChange parameter

ForceMode

Force:

Add force by AddForce function, the unit is N. If we keep calling in FixUpdate AddForce(Vector3.up *100,ForceMode.Force), 50 calls in that second (by default, FixUpdate is called once in 0.02 seconds) is to generate an upward force of 100N on the object for one second.
These two lines of code are the same

 rigidbody.AddForce(Vector3.forward*1.0f,ForceMode.Force);
 
 rigidbody.velocity += Vector3.forward*1.0f * Time.fixedDeltaTime / (rigidbody.mass);

Impulse:

Add impulse through AddForce function, the unit is N/s (Newton per second). Impulse is a force generated in an instant, and AddForce(Vector3.up *100,ForceMode.Impulse)1 call AddForce(Vector3.up *100,ForceMode.Force)is the same as the total force of 50 calls (one second).
These two lines of code are the same

 rigidbody.AddForce(Vector3.forward*1.0f,ForceMode.Impulse);
 
 rigidbody.velocity += Vector3.forward*1.0f / rigidbody.mass;

Acceleration:

Acceleration is added through the AddForce function. Ignoring the mass of the object, AddForce(Vector3.left*100,ForceMode.Acceleration)execute it within one second of fixupdate (default 50 times), that is, the object maintains an acceleration of 100 to the left within one second, and the stationary object will change to 100 after one second. These two
lines the code is the same

 rigidbody.AddForce(Vector3.forward*1.0f,ForceMode.Acceleration);
 
 rigidbody.velocity += Vector3.forward*1.0f * Time.fixedDeltaTime;

VelocityChange (variable speed):

The velocity change added by the AddForce function in m/s. Ignoring the object's mass, velocity change is a quantity that directly changes the object's velocity, which can affect the object's motion state immediately after a single call.
These two lines of code are the same

rigidbody.AddForce(Vector3.forward*1.0f,ForceMode.VelocityChange);
 
rigidbody.velocity += Vector3.forward*1.0f;

reference:

https://answers.unity.com/questions/802181/trying-to-understand-rigidbody-forcemode-derivatio.html?childToView=802667#answer-802667

Is it about quality

parameter quality related
Force yes
Impulse yes
Acceleration no
VelocityChange no

To deepen understanding, do some experiments

VelocityChange

Take a left flat throwing object as an example, the object is generated at (0, 0, 0). The initial velocity is 0, and there is gravity acceleration, so we only need to pay attention to the x-axis velocity.

private void FixedUpdate()
    {
    
    
        rb.AddForce(Vector3.left * 10, ForceMode.VelocityChange);
        //当对象y坐标小于0,摧毁该对象,并输出当前坐标
        if (transform.position.y < 0)
        {
    
    
            Destroy(gameObject);
            Debug.LogWarning("Position: " + transform.position);
        }

        //输出物体当前速度
        Debug.Log("Velocity: " + rb.velocity);
    }

insert image description here
Each call increases the x-axis velocity by 10. equivalent to

rigidbody.velocity += Vector3.left*10f;

Acceleration and Force

Take a left flat throwing object as an example, the object is generated at (0, 0, 0). The initial velocity is 0, and there is gravity acceleration, so we only need to pay attention to the x-axis velocity.

    private void FixedUpdate()
    {
    
    
        rb.AddForce(Vector3.left * 10, ForceMode.Acceleration);
        //当对象y坐标小于0,摧毁该对象,并输出当前坐标
        if (transform.position.y < 0)
        {
    
    
            Destroy(gameObject);
            Debug.LogWarning("Position: " + transform.position);
        }

        //输出物体当前速度
        Debug.Log("Velocity: " + rb.velocity);
        
       
    }

insert image description here
One call makes the speed in the x direction change by 0.2 (that is, one second call makes the speed in the x direction change to 10, which is consistent with Vector3.left * 10 in the code)

Acceleration can be understood as that the execution effect inside the function is equivalent to that the force whose parameter is Force is automatically multiplied by the mass of the object, that is, the following two codes have the same effect

rb.AddForce(Vector3.left * 10 * rb.mass, ForceMode.Acceleration);
rb.AddForce(Vector3.left * 10, ForceMode.Force);

That is, when the mass of the object is 1, the function whose parameter is Acceleration or Force is called, so that the object can obtain the same effect. Verify as follows

  • Acceleration
    insert image description here
    private void FixedUpdate()
    {
    
    
        rb.AddForce(Vector3.left * 10 * rb.mass, ForceMode.Acceleration);
        //当对象y坐标小于0,摧毁该对象,并输出当前坐标
        if (transform.position.y < 0)
        {
    
    
            Destroy(gameObject);
            Debug.LogWarning("Position: " + transform.position);
        }

        //输出物体当前速度
        Debug.Log("Velocity: " + rb.velocity);
    }
  • Force

insert image description here

    private void FixedUpdate()
    {
    
    
        rb.AddForce(Vector3.left * 10, ForceMode.Force);
        //当对象y坐标小于0,摧毁该对象,并输出当前坐标
        if (transform.position.y < 0)
        {
    
    
            Destroy(gameObject);
            Debug.LogWarning("Position: " + transform.position);
        }

        //输出物体当前速度
        Debug.Log("Velocity: " + rb.velocity);
    }

reference:

Myself

postscript

I wrote an article to search for information and did an experiment all night. I learned a lot, but the useful information is in English, and I haven’t found much in Chinese. So I want to share it with you who are also learning unity and looking for information on the Chinese Internet. , please click like, click favorite, thank you.

Guess you like

Origin blog.csdn.net/gongfpp/article/details/128886042