Time.deltaTime vs. Time.fixedDeltaTime in Unity
Notes:
Time.deltaTime vs. Time.fixedDeltaTime in Unity:
Note:
- Time.deltaTime must be used inside Update and LateUpdate methods
- Time.fixedDeltaTime must be used inside FixedUpdate method
- Time.deltaTime is used to update state of a game object per second instead of per frame
- Time.fixedDeltaTime is used to update state of a game object per physics update
W.K.T. generally in any game
deltaTime is a variable responsible for holding; time taken to execute a frame
deltaTime is used to update state of a game object independent of game's FPS value
deltaTime is used to update state of a game object per second instead of per frame
Time.deltaTime
- Time.deltaTime is most commonly used inside Update method
Example Code:
void Update ()
{
Debug.Log(Time.deltaTime);
}
- when used within Update method; it returns time passed in seconds; since the last Update method call i.e. how much time has been took to execute the last Update method call
- As game's FPS fluctuates, sometimes execution of last Update method call takes more time and sometimes less time; hence Time.deltaTime within Update method varies. Eventhough it varies roughly it updates game object per second.
- when used within FixedUpdate method; it returns time passed in seconds; since the last FixedUpdate method call i.e. how much time has been took to execute the last FixedUpdate method call
- As FixedUpdate is called after every 0.02s; Time.deltaTime within FixedUpdate method does not vary. It returns 0.02s.
Note: Time.deltaTime is used to update state of a game object per second instead of per frame.
Time.fixedDeltaTime
- Time.fixedDeltaTime must be used inside FixedUpdate method
Example Code:
void FixedUpdate()
{
Debug.Log(Time.fixedDeltaTime);
}
- When used inside Update or FixedUpdate mehod; it always returns time passed in seconds; since the last FixedUpdate method call i.e. how much time has been took to execute last FixedUpdate method call.
- As FixedUpdate is called after every 0.02s; Time.fixedDeltaTime does not vary. It always returns 0.02s.
Note: Time.fixedDeltaTime is used to update state of a game object per physics update