- Home /
how to get smooth slow motion?
I'm currently using the following script to create slow motion. However it doesn't smooth out the slow motion by adding the frame information for the frames in between the full speed motion and the new slower motion. By this I mean:
full speed has frames A,B,C,D
A true slow motion effect at 50% of real speed would create frames in between the above frames:
A, a, B, b, C, c, D, d
where the lower case frames are exactly halfway between the upper case letters in terms of all object positions and movements.
This is not the case for the below code, instead it does this at 50%
A, A, B, B, C, C, D, D
Where the second A is a repeat of the first A frame, not an effort to figure out the inbetween states of everything.
Is there a way to get true slow motion?
Here's the code I'm using;
var paused : boolean = false;
function Update () { if(Input.GetButtonUp("Jump")){ if(!paused){ Time.timeScale = .5; paused=true; }else{ Time.timeScale = 1; paused=false; } } }
Answer by Eric5h5 · Dec 22, 2010 at 09:38 AM
Setting Time.timeScale to .5 does result in "true" slow motion; no rendered frames are repeated. The problem you're seeing is that physics runs on its own discrete timer, 50 fps by default, which setting timeScale to .5 effectively halves. One thing you can do is to double the physics framerate to compensate. However, this chews up twice the CPU time when you're not using slow motion. Probably a better solution is to turn on interpolation for rigidbodies, in which case they're interpolated smoothly between each physics frame.
Not directly, but that's a good question, since that re$$anonymous$$ds me of the interpolation setting for rigidbodies, which I managed to completely forget about.
Hey Eric, think you've hit on something here, just about everything in the scene is under the influence of physics forces. Is there a way for me to change the physics discrete timer to ONLY increase during the slowmotion activity?
I've just poked around in the physics settings and cant find where to set the physics framerate...
Eric, you're a LEGEND. thankyou. Interpolation did the trick perfectly, however halved my overall frame rate :(
Answer by Statement · Dec 22, 2010 at 09:01 AM
Changing Time.timeScale cause Time.deltaTime to scale according to the time scale. For this to have any effect, you must ensure that all your movement/animation made in Update() properly use Time.deltaTime.
Example:
function Update()
{
transform.Translate(Vector3.forward * Time.deltaTime);
}
Note that you probably should change Time.fixedDeltaTime as the documentation for Time.timeScale suggests in order for your physics and other fixed time behaviors to work correctly.
You'd maybe think Unity could derive information somehow between two calls to Update but that is not how it works. It is up to you to scale animation with accordance to Time.deltaTime (which unity does scale for you).
I wish that was more straight forward in the documentation.
something like:::::::::: "wanna make slow motion? You need to slow down time, and the speed up the rate things are calculated. You slow down time by changing Time.deltaTime to a number lower than 1. You speed up the rate of calculation of physics and other occurrences during slowdown of time by decreasing the number at Time.fixedDeltaTime." Even this doesn't make logical sense, but will do for now, because it works... increased updates of physics calcs etc would have been better expressed as an INCREASE in rate rather than - time
I added the following to the above script, get a smooth slowmotion, but lose all physics control of my "characters" and frame rate drops by a factor of 10. Time.fixedDeltaTime = (Time.fixedDeltaTime / 5);
oddly enough, there's no mention in the literature of the default rate for Time.fixedDeltaTime, which means I had to do it this way.
Both above answers are right. One is right for physics and one is right for transform created movement... my question is at fault for not expressing which I wanted to remove the stuttering from.
Wrong again, both answers are right, full stop. Just took me to figure out the use properly. Can I give both answers a tick? It seems if I choose one it cancels out the other once...
Answer by KarlKarl2000 · Jul 01, 2016 at 05:47 PM
A very kind soul on the internet gave the answer on here
http://stackoverflow.com/questions/24344082/how-to-get-smooth-slow-motion-in-unity3d
The gist of it is to add these two lines
Time.timeScale = 0.5;
Time.fixedDeltaTime = 0.02F * Time.timeScale;
The fixedDeltaTime is what causes the smoothness to play out. If for whatever reason you need to revert the game back to normal speeds. I apply this after all the calculations are done.
Time.timeScale = 1;
Time.fixedDeltaTime = 0.02F ;
Seems to work for me.
Happy coding and hope this helps others.
This solved it perfectly. Didn't know fixedDeltaTime had a default value of 0.02 ins$$anonymous$$d of 1 as timeScale.
Be very careful, this solution destroys every physics calculation (e.g. AddForce), it may be better to use "Interpolate" on every rigid body as suggested by Eric5h5.
Answer by n8burba · Aug 15, 2021 at 09:15 AM
That solution, of multiplying Time.fixedDeltaTime, gives me physics glitches when pausing and unpausing my game.
The solution I came up with is to use Time.timeScale and then mimic FixedUpdate behavior in the Update loop using Time.realtimeSinceStartup:
float LastFixedUpdateTime = 0f;
void Start()
{
LastFixedUpdateTime = Time.realtimeSinceStartup;
}
void Update(){
if(Time.realtimeSinceStartup > LastFixedUpdateTime + Time.fixedDeltaTime)
{
LastFixedUpdateTime += Time.fixedDeltaTime;
ProcessFixedUpdate();
}
}