[unity] fixedDeltaTime + physics problems?
I'm using a very simple thing to make the game slow down and speed up, but it does really weird things to my physics - I've made completely sure that I've got no other forces acting on my game in code, and somehow, when I use these code to go slow and speed back up, some mysterious force acts on my objects (a bunch of jointed things).
Slow:
Time.timeScale = 0.05f;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
Return to normal speed:
Time.timeScale = 1f;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
I know it's the fixedDeltaTime line's problem, because if I take those lines out, the physics work fine. But the visual update becomes choppy.
What's going on?????? :X
Slow:
Time.timeScale = 0.05f;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
Return to normal speed:
Time.timeScale = 1f;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
I know it's the fixedDeltaTime line's problem, because if I take those lines out, the physics work fine. But the visual update becomes choppy.
What's going on?????? :X
Comments
https://docs.unity3d.com/ScriptReference/Time-timeScale.html
I had left it to be choppy but that's not ideal. I think that's okay for now but I really would like to know what the problem is :/
If I do implement the Time.fixedDeltaTime change then mysterious forces start affecting my stuff...
LateUpdate has to do with when things get updated. I'm trying to get a slow-motion effect on everything, and I don't see what LateUpdate has to do with achieving that?
I'm having trouble because the behaviour I observe in non-code generated physics are different between when the second line (Time.fixedDeltaTime = Time.timeScale * 0.02f;) is in effect or not. In theory the behaviour should be the same but slower, but instead I'm getting wonky forces that I didn't apply.
So I have tried this and I do not see the choppiness you described above.
Just a note: when you return to "normal" time you should set fixedDeltaTime back to its "default"
Time.fixedDeltaTime = 0.02f;
i.e. no need to multiply it by anything.
TimeStep = 1
Update 16ms
Physics 20ms
Update 32ms
Physics 40ms etc
Timestep = 0.25 without adjusting Time.fixedDeltaTime
Update Realtime Time 16ms Virtual Time 4ms
Update Realtime Time 32ms Virtual Time 8ms
Update Realtime Time 48ms Virtual Time 12ms
Update Realtime Time 64ms Virtual Time 16ms
Update Realtime Time 80ms Virtual Time 20ms
Physics 20ms
Update Realtime Time 96ms Virtual Time 24ms
What is more interesting it the weird forces you're seeing when you set fixedDeltaTime. Bear in mind if the physics time step is higher, you're getting a "more" accurate physics simulation as it's being integrated more often, and therefore it won't be EXACTLY the same as when it's running full speed. But I wouldn't expect to be seeing phantom forces. Perhaps you could post a gif of what's happening that shouldn't be happening?
By lowering the Time.fixedDeltaTime to 0.001 seconds, during slow motion, you are increasing the physics update to 1000 updates per second. At 60fps the update loop runs at ~16ms, which means in your current slow motion mode, you are updating the physics 16x per frame. I'm not sure how much physics you have in there, but I doubt 1ms is enough time to calculate anything substantial. Also, IIRC the physics updates are handled with an interrupt and having it happen every 1ms, your own code outside the fixed update function will be interrupted many times per frame.
Put this in LateUpdate:
Time.timeScale = Mathf.MoveTowards(Time.timeScale, targetTimescale, Time.unscaledDeltaTime * 3f);
And change targetTimeScale in Update as normal, along with Time.fixedDeltaTime = Time.timeScale * 0.02f;
My latest version works well enough with this :D
I tried scaling down the timescale to 0.05 and the fixeddeltatime accordingly in my physics cricket game, and the only difference I noticed was that the chains of joints seemed slightly stiffer, like they had extra angular drag on them (and of course everything was moving super slowly and smoothly). There were no phantom forces, and the scene had a few dozen joints in it (it had two cricketeers and some ropes hanging off of them).
I *think* that's what he meant :)
Smoothly changing the physics timestep/timescale as I suggested hides the problem a bit but does not make it go away.