Search Unity

Capping VSync Framerate

Discussion in 'Editor & General Support' started by bigpants, Jun 21, 2014.

  1. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    33
    For my game I need VSync ON and 45 FPS.
    I do NOT want HIGHER FPS. i,e. If the monitor refresh is 60fps, I still want 45fps
    Since TargetFrameRate is ignored when VSync is ON, does anyone have ideas how to do this?

    This is what I'm currently doing:
    Update()
    {
    totalDeltaTime += Time.deltaTime;
    if (totalTime > 1f / 45f)
    {
    // all the code lerps/slerps using totalDeltaTime rather than Time.deltaTime
    totalDeltaTime = 0f;
    }
    }
    It works but since totalTime goes over 1f/ 45f and set to zero, I do not get a perfect 45fps.
    It's more like 35fps, depending on the monitor refresh
    Even then I can't fully explain why it's not closer to 45fps.
    Important: I've got a proper frame counter (I'm not using unity's)


    Bonus
    If I keep VSync OFF and set TargetFrameRate to 45,
    and in Windows > Screen Settings > Graphics Card set VSync to ON for all applications...
    I get VSync at a solid 45fps (on a 60 Hertz monitor)
    Presumably it's just skipping 15 of the 60 frame updates... yes?


    History
    There's a possibility CapFPS solved this, but that thread fails to load. It's mentioned here:
    http://forum.unity3d.com/threads/fps-capping.18248/
     
  2. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    Just want to ask, why do you need to limit to 45 FPS? That's a weird place, I would recommend choosing between 30 or 60 (most game I would recommend 60).
     
  3. BigStink

    BigStink

    Joined:
    Sep 10, 2012
    Posts:
    11
    I think the CapFPS you're talking about is actually called FPSCap. You can find it here:
    http://forum.unity3d.com/threads/capping-framerate-or-stopping-100-cpu-utilisation.9668/

    Unfortunately, it doesn't seem to work for me for 45FPS. The logic seems sounds but apparently there are greater, darker forces in the shadows

    For me to get it around 45 FPS, I had to do:
    Thread.Sleep((int)(650 * (theDeltaTime - timeTaken)));

    Even then though, it's a bit up and down
     
  4. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    497
    There's no such thing as a 'solid vsync'd 45hz', not on an average 60hz screen at least.

    To get 45hz with vsync, you're rendering a mix of 1/60th sec frames and 1/30th sec frames. This may well look worse than just locking it to 30hz.
     
  5. bigpants

    bigpants

    Joined:
    Dec 9, 2009
    Posts:
    33
    Thanks so much everyone. Code and thoughts are exactly what I was looking for. I was worried there might be an easy/perfect away to solve this that I was overlooking. Now comes the compromise :)
     
  6. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    Would also like to say, usually running at a framerate that isn't a multiple of the monitor's rate (30, 60, 120, etc.) can cause screentearing and a few other probems, this video talks alot about framerate today.
     
unityunity