askvity

How to Fix Frame Rate in Unity?

Published in Unity Frame Rate Control 4 mins read

To fix or control the frame rate in Unity, you can primarily use the vSyncCount setting to synchronize rendering with the monitor's refresh rate, or you can set a target frame rate directly.

Unity provides developers with control over the application's rendering frequency. The two main methods involve using Vertical Synchronization (vSync) or setting a specific target frame rate.

Utilizing vSyncCount for Frame Rate Control

One effective way to manage your game's frame rate is by leveraging the vSyncCount setting. According to the reference, you can use vSyncCount to control the render rate relative to the monitor's refresh rate. This setting makes the engine render every n number of monitor refreshes.

  • How it works: When vSync is enabled (vSyncCount > 0), the game engine waits for the monitor to complete a refresh cycle before rendering the next frame. This prevents screen tearing but ties the game's frame rate to the monitor's refresh rate (or a fraction of it).
  • Practical Application: If your monitor runs at 60Hz, using vSyncCount allows you to render at divisors of 60Hz.
    • vSyncCount = 1: Renders at the monitor's refresh rate (e.g., 60 FPS on a 60Hz monitor).
    • vSyncCount = 2: Renders every 2nd refresh (e.g., 30 FPS on a 60Hz monitor).
    • vSyncCount = 3: Renders every 3rd refresh (e.g., 20 FPS on a 60Hz monitor).
    • vSyncCount = 4: Renders every 4th refresh (e.g., 15 FPS on a 60Hz monitor).

Here's a quick look at the relationship with a 60Hz monitor:

vSyncCount Target Frame Rate (on 60Hz Monitor)
0 Disabled (Runs as fast as possible)
1 60 FPS
2 30 FPS
3 20 FPS
4 15 FPS

You can set vSyncCount via code:

void Start()
{
    // Disable vSync to control frame rate manually (optional, often done before setting targetFrameRate)
    // QualitySettings.vSyncCount = 0;

    // Set vSyncCount to render every 2nd refresh (30 FPS on 60Hz)
    QualitySettings.vSyncCount = 2;
}

Or through the Project Settings > Quality interface:

  • Navigate to Edit > Project Settings > Quality.
  • Under the Other section, find the VSync Count dropdown.
  • Select the desired value (Don't Sync, Every VBlank, Every Second VBlank, etc.).

Using vSyncCount is generally recommended to avoid screen tearing and provide a smoother visual experience, especially on varying display types.

Setting a Target Frame Rate Directly

Another common method is to specify a desired frame rate using Application.targetFrameRate.

  • How it works: This setting attempts to make the application run at a specific number of frames per second.
  • Important Note: When QualitySettings.vSyncCount is set to a value other than 0 (i.e., vSync is enabled), Application.targetFrameRate is ignored. You must disable vSync (QualitySettings.vSyncCount = 0) for Application.targetFrameRate to have an effect.
  • Practical Application: You can set a specific frame rate like 60 FPS, 30 FPS, or even a higher value if the hardware can support it and vSync is off.

You can set the target frame rate via code:

void Start()
{
    // Disable vSync for targetFrameRate to work
    QualitySettings.vSyncCount = 0;

    // Set the target frame rate to 60 FPS
    Application.targetFrameRate = 60;
}

Setting Application.targetFrameRate = -1 allows the engine to run as fast as possible without a frame rate limit (when vSync is also disabled).

Choosing the Right Method

  • For VSync-based control: Use QualitySettings.vSyncCount. This is often preferred to prevent screen tearing and can lead to smoother animation synchronized with the display.
  • For a specific numerical target: Use Application.targetFrameRate after ensuring QualitySettings.vSyncCount = 0. This is useful for capping frame rates on powerful hardware or enforcing performance targets on less capable devices.

By understanding and utilizing these settings, you can effectively control and fix the frame rate of your Unity application.

Related Articles