Reduce framerate for server
Scripts in my game code contain FixedUpdate() and Update() calls. For the server-side code, only FixedUpdate() is needed since Update() only handles player input or graphics.
To conserve CPU, is it safe to bring the Application.targetFrameRate down to 5 fps (or something very small) and still have FixedUpdate() called independently? Or are FixedUpdate() calls somehow dependent on Update(), such as perhaps "FixedUpdate is only called right before Update as many times as needed"?
Thanks :)
Answer by fabian-mkv · Apr 29, 2016 at 08:37 PM
Since the documentation isn't particularly clear about FixedUpdate() and Update(), I played around with different settings to figure out what's going on. Here are the key takeaways:
1) Application.targetFrameRate will only take effect if V-Sync is disabled (Edit -> Project Settings -> Quality. In Inspector: Other -> V Sync Count -> "Don't Sync").
2) In Linux headless mode, Application.targetFrameRate takes effect without changing V-Sync (presumably since there is no V-Sync in headless mode)
3) FixedUpdate() gets only called immediately before Update() as many times as necessary. If Update() frame rate is higher than FixedUpdate() frame rate, then there may be Update() calls without being immediately preceded by a FixedUpdate() call. If Update() frame rate is lower than FixedUpdate() frame rate, then there may be multiple FixedUpdate() calls immediately before Update(). Either way, even if the actual time between FixedUpdate() calls may be 2ms (as read with Time.realtimeSinceStartup) instead of default 20ms (as defined by Fixed Time Step under Edit -> Project Settings -> Time), the Time.deltaTime in FixedUpdate() used for physics calculations is still the constant (e.g.: 20ms).
For purposes of running a headless server, not specifying Application.targetFrameRate uses the default value of -1. This means, do Update() calls as fast as possible. This causes unnecessarily high CPU usage since you don't need that many Update() calls. If you set Application.targetFrameRate to something like 5, then you will get around 10 FixedUpdate() calls every 200ms, which is also not good. The correct way is to set Application.targetFrameRate to the same as the FixedUpdate() framerate:
Application.targetFrameRate = 1f / Time.fixedDeltaTime;
Hope this helps someone.