How is Time.deltaTime calculated exactly?

This may seem like a very basic question, but the answer has been surprisingly elusive.

Exactly when/how is Time.deltaTime calculated?

  • Is it the time between the start of each frame i.e. a the start of the Physics update in the Order of Execution diagram or is it the start of each Game Logic section (before calls to Update)? or something else?
  • What is the value of Time.deltaTime for the first frame?

It frustrates me how vague the Unity documentation is about these kinds of technical details. They may be irrelevant to beginners, but can make a big difference when trying to diagnose weird corner cases. I’ve tried searching for answers, but the discussion threads are full of uninformed guesses rather than answers based on actual evidence or knowledge of the engine codebase.

Based on the player loop bindings, time is updated as literally the first part of the player loop: UnityCsReference/Runtime/Export/PlayerLoop/PlayerLoop.bindings.cs at master · Unity-Technologies/UnityCsReference · GitHub

As the docs state:

The interval in seconds from the last frame to the current one (Read Only).

Which I interpret as the time from the start of the last frame to the start of the current frame. That’s the only timing that makes sense to be perfectly honestly.

Thanks. So what does this mean for the second part of the question. What happens on the first frame?

I mean why not just write a 5 second script to test that yourself?

From my testing using this simple script:

namespace LBG.Core.Testing
{
	using UnityEngine;

	public class DeltaTimeLogger : MonoBehaviour
	{
		#region Inspector Fields

		[SerializeField]
		private int _logFrameCount = 5;
		
		#endregion
		
		#region Internal Members

		private int _currentFrameCount = 0;

		#endregion

		#region Unity Callbacks

		private void Awake()
		{
			if (_logFrameCount == _currentFrameCount)
			{
				enabled = false;
			}
		}

		private void Update()
		{
			Debug.Log($"Delta Time: {Time.deltaTime} (frame {Time.frameCount})");
			_currentFrameCount++;
			if (_currentFrameCount == _logFrameCount)
			{
				enabled = false;
			}
		}
		
		#endregion
	}
}

With this result:

I’m assuming it’s a constant value used during the first two frames of initialisation.

It’s worth mentioning here that Time.deltaTime will have a different value in the FixedUpdate loop that is unrelated to frame times - specifically it returns Time.fixedDeltaTime (see Unity - Scripting API: Time.deltaTime). REgards,

Andy