- Home /
About Forces and Time.deltaTime
Hello,
I'm a bit confused. Is there a need to use Time.deltaTime to calculate forces on Objects? Or is it only required if I calculate the position directly?
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
void FixedUpdate () {
if(Time.time < 10)
{
rigidbody.AddForce(Vector3.forward); // * Time.deltaTime ?
Debug.Log("Speed: " + rigidbody.velocity.magnitude + " Time: " + Time.time);
}
}
}
Time.deltaTime is used to do things in terms of time. It represents the time between frames and so is most useful in keeping timed things consistent.
Answer by InvincibleCat · Jan 23, 2015 at 08:02 PM
To clarify.
With AddForce you don't need Time.deltaTime because the physics engine will compute everything for you.
BUT if you do stuff like transform.Translate(amount). amount MUST depend on Time.deltaTime. So tranform.Translate(amount * Time.deltaTime)
It is important because even if your fixedDeltaTime is quite constant during the game. You may change it for performance reaons. So if you don't use Time.deltaTime, you will have to change all your values!
Also, never use Time.fixedDeltaTime. It is a good practice to always use Time.deltaTime (either on Update or FixedUpdate).
Answer by tanoshimi · Jan 23, 2015 at 07:24 PM
No.
FixedUpdate()
gets called at fixed time intervals (as determined by your project's physics timestep). So, calling AddForce()
in FixedUpdate, as you are currently, will apply a force at a given rate over several frames. If you were to multiply that force by Time.deltaTime
- the time it took to render the last frame - you'd just be adding an unnecessary, arbitrary, varying scale factor.
Time.deltaTime
is used in Update() - which, unlike FixedUpdate, can be called a varying number of times each second - in order to change variables at a rate that is independent of the number of times the function is called.
Ok, so you'd be multiplying by an unnecessary, aribtrary constant scale factor :)
Sorry but be careful, with AddForce, it is true, but Time.deltaTime $$anonymous$$UST be used in some cases in FixedUpdate
Answer by angularsen · Apr 02, 2018 at 03:13 PM
Detailed answer here: http://answers.unity.com/answers/1488426/view.html
TL;DR You do NOT need to multiply by deltaTime to get timestep independent physics.
Your answer
Follow this Question
Related Questions
AddForce not influenced by rotation 1 Answer
[Solved] Unity rigidbody.addForce ignores time.deltaTime? Possibly just a code bug I cannot find. 0 Answers
add force to object that has 2 different rigid bodies 0 Answers
[Solved]Why doesn't this Rigidbody.AddForce work the way I tell it to? 1 Answer
Make object tip over 1 Answer