How can you decide for how long a force is applied with AddForce()?

Hi,
I’d like to do a simulation of forces on bodies that sticks pretty closesly to reality (regarding physics). I calculate a Vector3 force and then want to apply it to a rigidbody.
This would be one possible solution:

using UnityEngine;

public class Showcase : MonoBehaviour
{
    // both Rigidbody and force are given/calculated somwhere else
    public Rigidbody body;
    public Vector3 force;

    // Update is called once per frame
    void Update()
    {
        // F = m * a <=> a = F / m
        Vector3 acceleration = force/body.mass;
        // v = a * t
        body.velocity += acceleration * Time.deltaTime;
    }
}

However this directly messes with Rigidbody.velocity, which you should do according to docs (Unity - Scripting API: Rigidbody.velocity).

They point to Rigidbody.AddForce() but I have trouble using it: If I add a force, I don’t have any control on how long it will be applied. However this very important for realism, a force’s effect is proportional to the time it’s applied for.
What am I missing here? How can you steer for how long a force is applied with AddForce()?

Thanks a lot!!!

Your code is the same as:

public Rigidbody body;
public Vector3 force;

    void FixedUpdate()
    {
        body.AddForce(force);
    }

By default AddForce uses ForceMode.Force which already takes mass and deltaTime into account.

BTW - you should add forces from within FixedUpdate. Although you can get away with adding a force from within Update if using ForceMode.Impulse.

Write a simple timer system to keep adding the force for x amount of time.

Thanks a lot, that was the information I was missing. And thanks for the fast answer + tip!