why using DeltaTime with AddForce ?

Hi all,
I’m a beginner and I am following the Roll-a-Ball project tutorial.
In the “Moving the player” step, he asks to use AddForce with Time.deltaTime to be frame independent.
I don’t understand why to use deltaTime. The manual says the AddForce parameter is either a force (mass.meter/second2) or an acceleration (meter/second2). So the parameter should not be a constant ? Why making it dependent of the frame rate ?
Thanks

The plot has been missed entirely on this one.

The reason to use Time.deltaTime in any function is to make it frame rate independent.

As FixedUpdate() runs at a fixed frame rate there are many people who will advise you not to bother with Time.deltaTime.

However changing the fixed frame rate is common when adjusting game speed, or for late stage optimisation. You really don’t want to have to adjust every line involving AddForce if you decide just before launch that you want a time step of 0.05 instead of 0.02.

TL;DR You do NOT need to multiply by deltaTime.

I just looked into this by creating a simple script, and found that FixedUpdate() indeed does add forces as if the delta time between each call to FixedUpdate()was multiplied into it.

Game object:

  • Mass m: 1
  • Gravity: 0
  • Linear/angular drag: 0
  • Position: (0, 0)
  • Force F: (0, 1)
  • Vertical acceleration: a = F/m = 1 / 1 = 1

By linear motion equations, t=5 seconds of vertical thrust should give velocity Vy = a*t = 1*5 = 5 and position Py = 0.5*a*t* = 0.5*1*5*5 = 12.5.

I tried running this with target framerate 5 FPS and 60 FPS, as well as fixed timestep 0.005 and 0.05s. The logs of the script matched the above calculations every time, meaning we DON’T need to multiply any time delta inside FixedUpdate().

using System;
using UnityEngine;

public class AddForce : MonoBehaviour
{
    private Rigidbody2D _body;
    public Vector2 Force;
    private float _lastTime;

    public void Start()
    {
        _body = GetComponent<Rigidbody2D>();
        if (_body == null) throw new Exception("No RigidBody2D attached.");
        _lastTime = Time.time;
    }

    public void FixedUpdate()
    {
        _body.AddForce(Force);
        Debug.LogFormat("Time: {5}, Add force {0}, velocity {1}, pos {2}, fixed time delta {3}, time since last {4}", Force, _body.velocity, _body.position, Time.fixedDeltaTime, (Time.time - _lastTime), Time.time);
        _lastTime = Time.time;
    }
}

Example output with timestep 0.05:

Time: 4.95, Add force (0.0, 1.0), velocity (0.0, 5.0), pos (-15.0, 12.4), fixed time delta 0.05, time since last 0.05000019
UnityEngine.Debug:LogFormat(String, Object[])
AddForce:FixedUpdate() (at Assets/Scripts/AddForce.cs:20)

Time: 5, Add force (0.0, 1.0), velocity (0.0, 5.0), pos (-15.0, 12.6), fixed time delta 0.05, time since last 0.04999971
UnityEngine.Debug:LogFormat(String, Object[])
AddForce:FixedUpdate() (at Assets/Scripts/AddForce.cs:20)

Time: 5.05, Add force (0.0, 1.0), velocity (0.0, 5.1), pos (-15.0, 12.9), fixed time delta 0.05, time since last 0.05000019
UnityEngine.Debug:LogFormat(String, Object[])
AddForce:FixedUpdate() (at Assets/Scripts/AddForce.cs:20)

Maybe I’m wrong but you should check this:
http://unity3d.com/learn/tutorials/modules/beginner/scripting/delta-time

First, deltaTime is reset to fixedDeltaTime when used inside Update. From the manual:

When called from inside
MonoBehaviour’s FixedUpdate, returns
the fixed framerate delta time.

So the tutorial is wrong. The only reason I could see for using Time.deltaTime in FixedUpdate() would be to match real-world physics. The use of ‘speed’ here is also wrong. You are applying force. So if you wanted to apply a measured force in Newtons to a known mass of the object, scaling by deltaTime would make the resulting acceleration be a much closer match to what you would get if you used a formula to calculate the acceleration.