- Home /
Inversing fixed delta time
void Awake()
{
initialFixedTime = Time.fixedDeltaTime;
}
void FixedUpdate()
{
Time.fixedDeltaTime = Time.timeScale * initialFixedTime;
fixedTimeFactor = 0.01f / initialFixedTime;
inverseFixedTimeFactor = 1 / fixedTimeFactor;
camForward = Vector3.Lerp(camForward, targetForward, 0.05f * inverseFixedTimeFactor);
}
This whole block of code is confusing.
What is the point of setting
Time.fixedDeltaTime
?What is the advantage over using
Time.deltaTime
?What is happening with all those factors?
Testing it, it does give a smooth interpolation, but what is really happening here?
Answer by FlaSh-G · May 28, 2018 at 12:57 PM
1.
fixedDeltaTime
is implicitly inverse scaled by timeScale
, while being responsible for the rate at which FixedUpdate is called. This means that if some other code would create slow motion by setting timeScale
to a lower value (than 1), FixedUpdate would be called less frequently. Multiplying fixedDeltaTime
by timeScale
would counter this effect, so you'd get the same amount of smoothness even in a slow motion situation. The downside is that FixedUpdate is useful for deterministic simulation, and changing its frequency according to a value that can be changed as easily as timeScale
will easily break that. I wouldn't recommend this step without a selfmade FixedUpdate cycle that would take over deterministic simulation. I'd personally prefer using that cycle for the camera though, and not change the builtin FuxedUpdate rate at all.
2.
In this case, Setting this value will not just change it for some multiplications, but change the frequency of FixedUpdate event calls.
3.
The calculations are pretty... useless. inversedFixedTimeFactor
is
1 / (0.01f / initialFixedTime)
which can be shortened to
100 * initialFixedTime
In the end, the value is used times 0.05
, so the final value can be shortened again to
5 * initialFixedTime
The 5 is nothing but a static speed factor that determines how fast the camera will move. Saving initialFixedTime
is just a workaround to get a Time.fixedUnscaledDeltaTime
, needed because fixedDeltaTime
is changed by that one line.
4.
So all this does in the end is a rather useless calculation of the value 5, multiplied by a constant value, while it changes the frequency of FixedUpdate to happen at a constant rate even when doing slow motion, which I'd not recommend to do. At least not with the builtin FixedUpdate.