Search Unity

FPS Capping

Discussion in 'Scripting' started by theinfomercial, Feb 21, 2009.

  1. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Hi guys! Anyone know of a way to limit the frame rate of a game down to a specified number such as 60 or 30? That way I can run my game at a solid framerate without my machine churning out 200+ frames a second. :D

    The editor is capped at 100 fps and the build is capped at 250. I would like to not have my machine spit out completely unnecessary frames if possible (some console games have something like this). Is there a way to do this in a script. :)
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There is no cap to 250, thats fully dependent on the hardware.

    Thought best is if you just enable "Sync to VBL" in the higher 4 quality levels (all but the fastest). That will ensure, that not more frames are rendered than the screen can render, ensuring that you do not get tearing which otherwise can be a pretty ugly issue.
     
  3. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Sure there is. I ran a build of a Unity game at 1920x1200 and the fps indicator read 230-235 fps. I changed the res to 1280x800 and it went to 250.00 and didn't go higher. Then I changed the res to 640x480 and the fps didn't go higher than 250. So yes there is, in fact, an fps cap. :D

    Anyway, I don't want to use sync to vbl (I might once I figure it out more), what I want is a way to cap the frame rate. Surely it's possible. :)
     
    SavvaMadar likes this.
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Aside from vsync (which is a perfectly valid way to cap the framerate as well as eliminate tearing; not sure why you don't want to use it), do a search for "fps cap" for another method. As far as caps go, I've seen up to 1000 fps for some of the simpler tests I've done (like this, though it only gets 800 fps in the webplayer). What you're seeing is not a cap in Unity; it's your hardware like dreamora said.

    --Eric
     
  5. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    No there isn't!!! ;-)

    ... seriously: My server goes up to 1000+ FPS if I let it. I don't know, maybe the 250 cap is some artefact of something else (could also be that I'm mistaken because my server has all cameras disabled ;-) ).

    Anyways, to answer your original question: There's a script called CapFPS lying around here on the forums, which simply uses Thread.Sleep(timeInMilliseconds). Let me see ... there you go:

    http://forum.unity3d.com/viewtopic.php?t=931

    Sunny regards,
    Jashan
     
  6. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    You mean that my hardware by default won't render past 250 fps, even if my hardware can theoretically render at a faster frame rate? Sort of like a screen refresh rate limit? That makes sense if OSX has it's own refresh limit built in somewhere I guess. But you say you are getting 800-1000 fps on some tests? How? :?

    I know in Windows that when I play Half Life 2, I can enable the Source engine fps indicator. The Source engine fps indicator can reach over 250 fps. So what, is it an OSX refresh feature/limitation? Or is there a limiter on the fps script that prevents it from going past 250 fps?

    Anyway, I tried out Vsync and it limited the fps to 60. So I'll use that for my game. The question now is if there is a way to limit the fps to 30 (for shooters that want the game to have a more cinematic frame rate). Anyone want to point me in the right direction? :D

    Thanks for the help with this guys!

    Edit: Jashan, you're the best. :D
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    For that particular scene you're testing, it won't. With graphics cards, there's usually a point where decreasing resolution won't get you any increase in FPS, unless you're really heavy on the pixel shaders. Nope, there isn't an OS X limitation or anything.

    By not having much going on in the scene. :) Why not give the link above a try and see what you get? (In full-screen mode).

    --Eric
     
  8. Diablohead

    Diablohead

    Joined:
    Feb 3, 2009
    Posts:
    47
    250fps is overkill anyway :D

    Surely Unity can force a fps cap somehow, since some pc's and macs are not power beasts it can be a better experience to limit frames to say 30fps instead of it going between 30 and 60 constantly.
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    That jumping between them is exactly the reason why I recommend to disable sync to VBL for the lowest quality (or lowest two). Those machines are already "crap" so they won't have the chance to render faster than screensync. So with vbl enforcement, they likely will fall to 1/2 of the screensync when they can't maintain stead screensync rates.

    Beeing able to specify max fps thought would be great.
    The majority of ways to enable that also directly impact the logic processing which is bullocks.
    To explain why: assume that you have a system thats hardly capable to handle rendering and logic at 60 FPS. So it would be great if the user could specify 40 FPS (vbl off) as hard max, similar to how already Unreal Tournament supported it loooong ago.
    Now the rendering is capped to 40 FPS.
    But as a mather of fact, this lightens the cpu burden, allowing your game to have more stuff going on especially in terms of physics and AI

    Its not the FPS counter that makes the gameplay but having a technology that enables you to put the "focus" on the right thing your game.
     
  10. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    Well I just tested an empty scene with an fps counter and it went right up to 1000 fps (which I guess is the "True" fps cap for the engine). So yeah, Eric was right (again! :evil: :D ). When the game went to 250 fps and didn't go above, I guess I thought that was the limit (because it "looked" capped).

    Thanks for the help guys. I'm going to try that cap script now and see if it will work. :D

    Edit: It works. :)

    Edit 2: Doesn't really work that well (it's hit or miss depending on what's being rendered). Anyone want to make me an fps capping script? Puhleeeeze? :(
     
  11. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    @theinfomercial: there's no FPS cap in the standalone. It's not 250FPS, and it's not 1000FPS; there's just no cap. Probably your CPU, GPU, RAM or anything else just can't get faster than that on that particular scene.

    That said, wait a bit for Unity 2.5. FPS cap will be there. (ouch... did I just reveal one more upcoming feature?)
     
    MurphyMurph_21 likes this.
  12. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    hehe yeah just make fun of us :p

    Naw great to know that there is a bit of core non-editor functionality coming as well, sounds great what we know so far :)
     
  13. theinfomercial

    theinfomercial

    Joined:
    Sep 9, 2008
    Posts:
    1,000
    You guys are the best. :D

    But I guess that means you haven't told us everything about 2.5 then, right? :wink:
     
  14. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Well, you haven't asked about everything :)
     
  15. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    That'll be handy! I can remove the FPS limit hack I was using at that point.
     
  16. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Hey Aras, tell us everything! NOW! We must know! It is extremely important!!!

    :twisted:
     
  17. moon cresta

    moon cresta

    Joined:
    Feb 25, 2009
    Posts:
    40
    hi,

    2 doubts:

    "wait a bit for Unity 2.5. FPS cap will be there"
    "If you want to cap the maximum frame rate to save the planet - now you can!"
    1.
    in editor(where) or via script(how)?

    2.
    it doesn´t work if i disable sync to VBL
    the editor still runs at 60 fps
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Application.targetFrameRate.

    --Eric
     
  19. moon cresta

    moon cresta

    Joined:
    Feb 25, 2009
    Posts:
    40
    thanks.

    it doesn´t work if i disable sync to VBL
    the editor still runs at 60 fps