- Home /
Run Update at a constant framerate in a single script
I have a ghost script that replays a users car movement. If it records at 30FPS and then performance increases to 60FPS the recording plays twice as fast. Is it possible to tell my update in my ghost script to run at like 15 FPS?
I don't require a high FPS as the tween between frames is fine. Also the 30 and 60 FPS are just example numbers.
The rest of my game can run at whatever FPS it requires but I want my ghost script to be fixed so the replay is always the same speed.
Will FixedUpdate do what I need?
Thanks
Answer by IvovdMarel · Oct 16, 2018 at 12:47 AM
Yes FixedUpdate will do what you need.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
https://unity3d.com/learn/tutorials/topics/scripting/update-and-fixedupdate
FixedUpdate is actually the physics tick and should only be used for that. Changing the fixedDeltaTime will affect physics simulation. By default the physics framerate / deltaTime is set to 50 fps (0.02s).
Answer by Bunny83 · Oct 16, 2018 at 12:52 AM
Instead of FixedUpdate you may want to use a custom fixed update callback.
I might try this and save on unnecessarily frequent saving. I think i can get away with 10 times per second rather that however many fixed update runs. Thanks
Answer by Lagger625 · Oct 16, 2018 at 12:57 AM
FixedUpdate can do the trick, it runs at 50 FPS with default fixed timestep setting, wich is 0.02. Or you can use coroutines for this. Like following pseudocode:
void StartPlayback(){
StartCoroutine(Playback());
}
IEnumerator Playback(){
do {
///////////////////////
// playback logic here
///////////////////////
yield return new WaitForSeconds(1f / 30f);
} while (playbackNotFinished);
}
void StopPlayback(){
StopCoroutine(Playback());
}
Coroutines wouldn't be very accurate for this, especially when the framerate isn't at least some multiples higher than your desired rate. $$anonymous$$y custom fixed update ensures a certain call count per second. You can make a method to run 10000 times per second or just once every 10 seconds.
Your answer
Follow this Question
Related Questions
Camera ignoring LOD! 0 Answers
Low FPS on Simple geometry? 4 Answers
Unity 2017 frame rate capped? 2 Answers
Game Freezes upon successive loads 0 Answers