- Home /
Setting fixeddeltatime depending on TimeScale makes physics behave weirdly
I ran into a Physics issue when setting the fixedeltatime timestep at runtime according to the current Time.TimeScale. I frequently change the TimeSclae in my project at runtime in order to create slowmotion and bullet time effects. In order to also have smoothly working Physics and other features depending on the fixeddeltatime, I also set Time.fixedDeltaTime accordingly (Time.fixedDeltaTime = 0.02f x timescale). While this indeed gives me smooth behaviour even during the slow motion I ran into related issue that I cannot seem to solve: Applying a force to a rigidbody does not seem to work at all times anymore. This also occures if the TimeScale is 1 and thus fixeddeltatime = 0.02f.
I tried to debug the problem and it seems like it is related to setting fixeddeltatime at runtime, regardless of the new fixeddeltatime value. This problem even occures when time fixeddeltatime is only set once at the start to the default value of 0.02f.
In short: setting fixeddeltatime manually (even setting it once at the start of the game to the same default value of 0.02f as it already is makes physics work in a weird way, which leads to forces sometimes not being applied properly)
Does anybody have an idea how to resolve this issue? If need I can provide video material and code examples
Answer by Edy · Jul 22, 2020 at 04:40 PM
Don't change fixedDeltaTime. Doing so will change the physics behavior in unpredictable ways. Change Time.timeScale alone to get physically coherent behavior regardless how often timeScale is changed, or its value.
You can get visually smooth motion at any time scale by enabling Interpolation in the rigidbody.
Hm okay. As far as I had known it is recommended to do that but I might be wrong. How would you go about other things that depend on the fixeddeltatime? For example Cinemachine updating, other code that has to be run in fixedupdate etc?
They should either be updated in Update using Time.deltaTime, or implement their own interpolation in Update.
Cameras should be updated in Update or LateUpdate to adapt to the visual frame rate, not the physics frame rate. If the systems shouldn't slow down with the time scale (i.e. camera motion should be the same speed regardless time scale) then they should update in Update and use Time.unscaledDeltaTime.
If the system is updated in FixedUpdate but it requires smoothly visuals then they should interpolate their pose in Update, then apply the interpolated pose to the visual object. For example, in this video:
https://www.youtube.com/watch?v=KZmEEynnG6$$anonymous$$
The second part is slow motion. Wheel rotation is updated in FixedUpdate, but the visual wheel mesh is rotated in Update using values interpolated from the last two FixedUpdate cycles.
Alright I looked into it and it seems your workaround seems to work really well. Thank for taking the time to help me out!