- Home /
Slow motion for Physics game
I recently decided to add slow motion to my game, which can be activated at will by the player. I have no problem getting the desired effect, but my problem is either the physics or performance... let me explain:
The Physics It seems that the most common way to do slow motion is to adjust Time.timeScale, Time.fixedDeltaTime and Time.maximumDeltaTime, as I do in the snippet below, where .02f and .15f are the default values for fixedDeltaTime and maximumDeltaTime respectively.
Time.timeScale = enableSlowMo ? .2f : 1;
Time.fixedDeltaTime = Time.timeScale * .02f;
Time.maximumDeltaTime = Time.timeScale * .15f;
This looks really good, but the downside is that because I make changes to fixedDeltaTime, the physics calculations are wrong, and the objects more differently in slow motion than at normal speed.
The Performance Issues My second solution was to just change Time.timeScale, this gives very jagged movement of most objects while in slow motion though. As such, I decided to enable interpolation on all rigidbodies, and voila, I have my desired result. The problem is though that in some of my levels I have several hundreds of rigidbodies (It's a physics game after all) which drastically affects performance when interpolation is enabled on all rigidbodies, especially in cases where a lot of collisions occur.
My own thoughts are that there may be some way to "calculate" my way out of the issues generated by the first solution, without cutting too much into performance, but I'm still coming up short.
In short, my question is: Are there better ways to do this, than the two I've mentioned?
Any input or hints would be greatly appreciated - Thanks for your time.
I don't think you should modify Time.fixedDeltaTime
, especially that it is already affected by Time.timeScale
(see note in scripting reference).
I think the second way is the correct approach, but unfortunately I don't know how to optimize it.
Answer by KittenSnipes · Jan 22, 2018 at 05:43 AM
@NasarethMekuri
Well Brackeys seems to get smooth results so I am just going to send you his video rather than explain it because he explains it much better than I can.
Here's the video: Video Link
@$$anonymous$$ittenSnipes Thanks for your reply.
Brackeys solutions is more or less the same solution as the first one I mentioned myself, the only difference is his approach and that he doesn't change the maximumDeltaTime. - The end results are the same, the issue is just not that obvious in his example. The problem is if I shoot an object forward using physics, as soon as it collides with something, the resulting trajectory is different between the slowed timescale and the normal, because of the changes to fixedDeltatime.
As I mentioned in my post, I have a "fix" which is not changing fixedDeltaTime and using Interpolation on all my rigidbodies, but it's fairly expensive in my case.
I was hoping for alternatives to the two suggestions, or at least modifications to them :-)