- Home /
How to limit FPS?
According to this article: https://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html I've put this code:
void Awake() {
QualitySettings.vSyncCount = 1;
Application.targetFrameRate = 30;
}
into my only one camera's script. In the stats of the Game window I see 96-110 FPS. How to limit it to 30? I use the latest Unity on the latest MacOSX.
Answer by Xarbrough · Mar 02, 2018 at 02:46 PM
As others have mentioned, you disable vSync and set the targetFrameRate like this:
using UnityEngine;
public class SetTargetFrameRate : MonoBehaviour
{
public int targetFrameRate = 30;
private void Start()
{
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = targetFrameRate;
}
}
However, you have to test the result on device to see if it works. The documentation doesn't say anything about the game view render update in the editor, but it does say that the results are different per platform.
I've tested the code in the editor and it was working, but we need to understand that it's not a hard limit, but just a recommendation. When I set it to render at 30, I get results up to 40 frames per second displayed in the stats panel. This may be because the stats panel doesn't measure fps accurately or because the final frame rate may be a little higher on standalone. In any case, I would measure fps on device with one of the official profiling tools, e.g. Apple's XCode for iOS devices. There, the limit of 30 vs 60 fps works accurately (at least when I tested last).
Answer by Brogan89 · Jun 17, 2017 at 12:15 PM
Why would you ever want to limit FPS?
EDIT To be more helpful, and actually provide an answer :)
public class FPSTarget : Monobehaviour
{
public int target = 30;
void Awake()
{
QualitySettings,vSyncCount = 0;
Application.targetFrameRate = target;
}
void Update()
{
if(Application.targetFrameRate != target)
Application.targetFrameRate = target;
}
}
To decrease battery usage on mobile devices if I don't need fps to be around 60. Or I can use dynamic FPS ins$$anonymous$$d.
This isn't an answer, please remove it and post it as a comment ins$$anonymous$$d :)
yea my bad. Settings targetFrameRate should be they way though.
Do something like this.
public class FPSTarget : $$anonymous$$onobehaviour
{
public int target = 30;
void Awake()
{
QualitySettings,vSyncCount = 0;
Application.targetFrameRate = target;
}
void Update()
{
if(Application.targetFrameRate != target)
Application.targetFrameRate = target;
}
}
might be over kill checking every frame but it will work
As a side note, there is a comma on line 8 that should be a period. Other than that this works fine :) Cheers!
this surely worked for $$anonymous$$e I wanted to set the fps around 60 so I made the target FPS to 70
Answer by Max_Bol · May 23, 2021 at 04:02 PM
For those who still wonders how the targetFrameRate works, it's not an absolute value.
There's a lot of information on the API description here.
For those who still don't get it, the value may be partially or fully ignored for multiple reasons. First, in case you're enabling VSync (above 0), the framerate will be ignoring the targetFrameRate value (as explained here) and will, instead, use the system default framerate. If your device has a default FPS of 120 with VSync at 1, it will try to render at 120 fps regardless of targetFrameRate. If you set VSync to 1-4 (max value applicable is 4), the fps will be equal to the device's default framerate divided by the VSync value. So, if you set it to 2 and your device has a default framerate of 120, it will be render at 60 fps. At the same time, if it's a default 30 fps, it will drop at a max of 15 fps.
PCs default framerates are determined by the GPU driver and can be accessed by the player/user through their GPU interface. You can't change it from within the game so, if you allow VSync in some options menu, it's a good practice to put some sort of notice/info panel when VSync is enabled to make the player know that he or she has to do the change while the game is not running. On mobile device, 95% of the devices doesn't allow the user to change the default framerate. Some manufacturers will allows it through the device's screen refresh rate in the devices' screen advanced settings. In the case of consoles (for those who wonders), both the Xbox X and S have default framerate of 60 while the PS4 is set to 60 and PS5 is set to 120. It's possible to make the Xbox X render at 120, but it requires a particular plugin and I'm not sure if it's compatible with the way Unity determines the device's default framerate.
Answer by VinnieH01 · Jun 17, 2017 at 11:31 AM
Set the vSyncCount to 0
Unfortunately it doesn't work. QualitySettings.vSyncCount = 0; Application.targetFrameRate = 60; gives me 90-95 fps
Your answer
Follow this Question
Related Questions
How to limit FPS on non-retina devices? 0 Answers
targetFrameRate & vSync for iOS 3 Answers
FPS shoot mechanic works in editor, but not after build! 2 Answers
How do I make my bullet tracers show when shooting in the air? 0 Answers
How can I make a Grapple Gun work with the Character Controller? 1 Answer