Time.unscaledDeltaTime is very useful yet it has some drawbacks in the Editor. For example it starts running early if one hit’s the play button (ticks up while domain is reloading) and it also keeps increasing if the editor is paused.
This causes some confusion, see:
Here is a little helper that takes care of it in the Editor (at runtime it will simply return the normal Time.unscaledDeltaTime).
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace YourNamespace
{
public static class DeltaTimeUtils
{
public static float unscaledDeltaTime
{
get
{
#if UNITY_EDITOR
// Lot's of shenanigans to make unscaled delta time work as expected in the editor, see:
// https://discussions.unity.com/t/time-unscaledtime-confusion/675468
if (EditorApplication.isPlaying)
{
// Avoid division by zero
if (Mathf.Approximately(Time.timeScale, 0f))
return Time.unscaledDeltaTime;
// Why not also use Time.unscaledDeltaTime here? Because in the first frame after unpausing in the
// editor the unscaledTime has a big value (duration of the pause).
return Time.deltaTime / Time.timeScale;
}
#endif
// In builds if it is the first frame(s) then do not use unscaledDeltaTime since
// it is potentially very big here (splash screen time etc.).
if (Time.frameCount <= 1)
return Time.deltaTime;
else
return Time.unscaledDeltaTime;
}
}
}
}
Usage
float deltaTime = DeltaTimeUtils.unscaledDeltaTime;
May it be useful ![]()