- Home /
If timeScale is lowered how can I aviod to get the physics simulation choppy?
A simple script attached to a gravity dragged falling cube:
Time.timeScale=0.1;
And it's fall isn't smooth. Yuk! How could I smooth it out?
(Using Unity 3.1)
Answer by duck · Dec 03, 2010 at 03:32 PM
You need to reduce the physics timestep ( Time.fixedDeltaTime ) value by a corresponding amount.
If I'm doing slow-motion, I usually do something like this:
Time.timeScale = slowMotionSpeed;
Time.fixedDeltaTime = slowMotionSpeed * 0.02f;
0.02 is the default physics timestep, so this code means the physics maintains its smoothness in proportion to the current timeScale - so you'll get 50 physics steps per frame, regardless of the timescale.
What should I do when restoring the Time.timeScale to 1.0? Do I need to do something like Time.fixedDeltaTime = slow$$anonymous$$otionSpeed * 1.0f? Or does fixedDeltaTime automatically come back to normal when timeScale is 1.0?
I got it... when restoring timeScale to 1.0, set fixedDeltaTime to 0.02 which is the default value, or whatever value you have set under Edit - Project Settings - Time - Fixed Timestep.
Seems like you also need to set Time.maximumDeltaTime to whatever your time scale is.
Time.timeScale = slow$$anonymous$$otionSpeed;
Time.maximumDeltaTime = slow$$anonymous$$otionSpeed;
Time.fixedDeltaTime = slow$$anonymous$$otionSpeed * 0.02f;
When I was using slow motion on ragdolls without that line, the ragdolls would begin to float up into the air and would shoot off like rockets when I turned slow mo off.
Reference: http://www.41post.com/4258/program$$anonymous$$g/unity-slow-motion-and-rigidbody-behavior
"Seems like you also need to set Time.maximumDeltaTime to whatever your time scale is."
That's not what the article is saying. It's saying you need to factor both maximumDeltaTime and fixedDeltaTime by the same value.
newTimeScale = Time.timeScale/slowFactor;
Time.timeScale = newTimeScale;
Time.fixedDeltaTime = Time.fixedDeltaTime/slowFactor; //<<<<<<<<<<<<<
Time.maximumDeltaTime = Time.maximumDeltaTime/slowFactor;//<<<<<<<<<<
(Worth pointing out that this method wont work properly if you want to lerp the scale over time.)
If I also change the fixedDeltaTime my car wheels rotates fast without dragging force. In other words: The physics behavior is weird / glitchy. If I only change the timeScale value it is choppy. But I think I have to increase the physics interpolation ins$$anonymous$$d of change fixedDeltaTime. Is there a variable for the physics interpolation? $$anonymous$$aybe the interpolation attribute of each rigidbody. Or is there a global variable?
Answer by Waffle1434 · Mar 09, 2018 at 11:59 PM
Another Solution would be to enable Interpolation on rigid bodies. This more or less costs the same, but guarantees smooth results.
Your answer
Follow this Question
Related Questions
calculate time required if timescale is changed 0 Answers
Cannonball physics? 1 Answer
Physics behave weirdly after timeScale change 1 Answer
Time Scale and physics problem 2 Answers
Physics calculations vs TimeScale vs FixedDeltaTime 1 Answer