- Home /
any way to *not* use deltaTime when framerate slows?
Let me explain. I'm looking for what is essentially a 'hybrid' timestep. I'm making a bullet hell game which requires very little incongruencies with bullet patterns and speed. As a result, when the framerate slows down past a certain target framerate (say, 60 fps) I would prefer my systems run without taking delta time into account. So calculations would be taken step by step and the simulation would run more slowly, which is exactly what I want. On the other hand, once it's past the target framerate, I would like it it to maintain the delta, so it just moves more smoothly without speeding up.
Any suggestions for this, or solutions like it?
http://www.kinematicsoup.com/news/2016/8/9/rrypp5tkubynjwxhxjzd42s3o034o8
TLDR: use fixed time for physics simulation. delta time for camera/render stuff
Answer by Bunny83 · Oct 11, 2018 at 08:09 AM
Well, there's an easy fix for your hybird solution ^^ just clamp deltaTime properly. That means it should never grow beyond "1 / 60" (-> 0.016666).
float dtFixed = Math.Min(Time.deltaTime, 1f/60f);
Note that this only fixes things you control and where you use dtFixed instead of Time.deltaTime. This could be implemented in a seperate static class
public static class MyTime
{
public static float maxDT = 1f/60f;
public static float TargetFrameRate
{
get { return 1f / maxDT}
set { maxDT = 1f / value;}
}
public static float deltaTime
{
get
{
if (Time.deltaTime > maxDT)
return maxDT;
return Time.deltaTime;
}
}
}
Now you can simply use MyTime.deltaTime instead of Time.deltaTime. You can change the target framerate be either setting MyTime.maxDT or MyTime.TargetFrameRate. This essentially returns a fix dt value (maxDT) when the framerate drops below your target framerate.
Note as i said above this only fixes things which will use your deltaTime. For example physics runs on a fixed framerate and isn't affected by deltaTime at all. Particle systems and the animation system runs with Unity's internal deltaTime value. Those are not affected by this of course.
You could however instead of using your own dt try to change the Time.maximumDeltaTime (and probably Time.maximumParticleDeltaTime as well). However Unity "advices" you to not go outside the range 1/10 and 1/3 (default value is 1/3). So we don't know what happens when you set it to 1/60. Since physics is also affected by this and the default physics step is 1/50 it may completely disable physics or other weird things could happen. You have to try it.
Another way, if the above fails, could be to manually adjust the timeScale to compensate the deltaTime for low framerates. Though it's tricky to find the "right" place where to adjust the timescale. The Script execution order may help here.
public class LimitDeltaTime : MonoBehaviour
{
public float TargetFrameRate;
void Update()
{
float scale = Time.unscaledDeltaTime * TargetFrameRate;
if (scale > 1)
Time.timeScale = 1f / scale;
else
Time.timeScale = 1f;
}
}
Note this is untested. Changing the timescale like this could cause other issues. Also keep in mind if you want to change the timescale yourself you have to include that in this script since we set the timescale every frame. Replace the "1f" with your desired timeScale
Wow! Thank you for the very thorough answer! I will try these solutions out but it seems to be exactly what I'm looking for.
Answer by MSavioti · Oct 11, 2018 at 07:14 AM
Have you tried using fixedDeltaTime?
fixedDeltaTime is just a constant. It never changes unless you change it. It dictates the framerate of the FixedUpdate method.
Your answer
![](https://koobas.hobune.stream/wayback/20220612180134im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Jump not framerate independent 3 Answers
Framerate independent character movement 1 Answer
Framerate independent physics (FixedTimestep, FixedDeltaTime) 1 Answer
Framerate independent Movement 2 Answers
FixedUpdate and Time.deltaTime slowe 2 Answers