Make your game frame rate independent
Tutorial
·
Beginner
·
+0XP
·
20 mins
·
(2594)
Unity Technologies

A game’s frame rate can have a significant impact on player experience. In this tutorial, you'll learn how to make your game frame rate independent to improve the experience of players with older hardware.
1. Overview
The frame rate of a game is how often the image (frame) is updated when that game is rendered (displayed) on a screen. Frame rate is also sometimes called the update rate, and you may have used the Update function included in Unity’s default C# script template — this function is called every time a new frame is displayed.
Frame rate can impact game performance and player experience, so you need to make considered choices regarding your game as a creator.
By the end of this tutorial, you’ll be able to do the following:
- Explain different factors that impact game frame rate.
- Explain the advantages and disadvantages of frame rate independence.
- Configure your game frame rate to be independent.
Working on your own project?
This tutorial is part of Beginner 2D: Adventure Game and refers to the game project for that course, but this information will be helpful for any beginner creator who wants to make a frame rate independent game.
Note: For the purposes of this tutorial, we chose to use the Ruby’s Adventure asset set, and the file paths used in instructions will reflect this. If you chose another asset set, the file names will be the same, but in your corresponding theme folder.
2. Frame rate and player experience
Frame rate, which is generally measured in frames per second (fps), can have a big impact on the player experience of a game. When a game has a low frame rate, it can appear laggy and slow for players; there’s a significant difference between 20 frame updates per second and 100 updates!
What impacts the game frame rate?
Game frame rate can be impacted by a number of different factors, including the following:
- The player’s system hardware: This hardware includes computer memory and graphics cards. Consoles will have standardized hardware, but a Windows or Linux PC’s hardware can vary considerably — your player could have very new hardware made for intensive gaming or much older hardware specifications.
- The player’s settings: Players are often able to choose the quality of graphics rendering and resolution for commercially released games. Making considered setting choices can help players with lower-specification hardware maintain a good frame rate.
- Game optimization: As a creator, there are choices that you can make when optimizing your project to ensure a good frame rate for as many players as possible. Some choices relate to how you configure your game graphics and rendering, including establishing a fixed framerate.
3. Test the default 2D Beginner movement
Player character movement in the example scene for the 2D Beginner: Adventure Game course is currently linked to the frame rate: the character moves in units per frame. If you’re following the course and creating your own adventure game, this is how your game is currently configured too.
If one player has a very old computer that can only run the game at 30 fps and another player has a computer that can run it at 120 fps, those two players will have a main character that moves at very different speeds. The frame rate will make the game either harder or easier to play, depending on the machine running the game and the player’s in-game objectives.
To test the impact of frame rate on the 2D Beginner game, follow these instructions:
1. In your 2D Beginner Unity project, open the scene that you have been working on, then enter Play mode and test the player character movement speed. By default, this game runs at 60 fps.
2. In the Project window, navigate to the Scripts folder and open the PlayerController script in your IDE.
3. At the beginning of the Start function, add the following instructions:
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = 10;These instructions will make Unity render the game at 10 frames per second.
4. Save your changes and return to Unity Editor. Enter Play mode and test the character movement at 10 fps — you will probably find it very slow! Remember to exit Play mode when you’re done.
Why does the frame rate impact the character movement?
The player character movement is calculated in the Update function, which is called every frame:
void Update()
{
Vector2 move = MoveAction.ReadValue<Vector2>();
Debug.Log(move);
Vector2 position = (Vector2)transform.position + move * 0.1f;
transform.position = position;
}
If your game runs at 60 fps, the character will move at a rate of 0.1 * 60, which gives six units of movement per second. But if the game runs at 10 fps, like you just forced it to do, the character only moves at a rate of 0.1 * 10, so one unit per second! The difference can result in a poor player experience for some players, particularly those with old hardware.
You can address this problem as a creator by making your game run at the same speed, no matter what the player’s frame rate — this makes the game frame rate independent and provides a more consistent player experience.
4. Adjust the movement to units per second
A game’s frame rate is variable, but a fixed measurement of time is much more reliable: one second always has the same duration. Adjusting the player character movement to be measured in units per second will mean that the movement will always be the same speed, whatever the player’s hardware.
To adjust the movement measurement, follow these instructions:
1. Return to the PlayerController script in your IDE.
2. Add two forward slashes (//) to the beginning of the instructions you added to the Start function:
//QualitySettings.vSyncCount = 0;
//Application.targetFrameRate = 10;
Two forward slashes are used to identify comments. Comments are notes in your code that the compiler, which processes the instructions for Unity to execute, will ignore. Comments can be useful for adding notes for yourself or collaborators, and for stopping instructions being executed without deleting code you have written.
3. Find the instruction that defines the position variable. Make the following adjustment:
Vector2 position = (Vector2)transform.position + move * 0.1f * Time.deltaTime;Here’s an explanation of your change:
- deltaTime, contained inside Time, is a variable that Unity uses to store the time it takes for a frame to be rendered.
- You can use deltaTime as a multiplier to calculate how much the GameObject needs to be moved to ensure that it will move 0.1 units per second.
- Review the subsection below if you want to review how Time.deltaTime works in more detail.
4. Save your changes and return to the Unity Editor.
5. Enter Play mode and test the character movement. Hold the key or joystick input for a few seconds until the character begins to move — this movement will be very slow. Exit Play mode when you’re done.
Why is the character so slow?
Although the character is very slow, your code is actually working as expected. The current value to increment (increase) the position is 0.1, so the character moves 0.1 units per second and takes 10 seconds to move a full unit.
Time.deltaTime calculation example
If you need a little more guidance to understand how Time.deltaTime works in the calculation, the following example may help.
A frame rate of 60 fps mean delta time is 0.017s, as 1/60 is approximately equal to 0.017. 0.1 * 0.017 = 0.0017 unit moved per frame. After 60 frames (so 1 second in time), the GameObject will have advanced 0.0017 units 60 times. 0.0017*60 approximately equals (~=) 0.1 unit.
If the game runs at a frame rate of 10 fps instead, deltaTime will be 0.1s (1/10 = 0.1).
0.1 * 0.1 = 0.01 unit moved frame. After 10 frames (1 second in time), the GameObject will still have moved 0.01*10 = 0.1 unit.
When you use Time.deltaTime, the player character will move the same amount per second no matter what the frame rate is.
5. Adjust the speed of movement
A movement rate of three or four units per second will produce a reasonable running speed for the player character, but you can experiment to find a speed that feels right for your game.
To adjust the script, follow these instructions:
1. Return to the PlayerController script in your IDE.
2. Adjust the movement increment value to 3.0f:
Vector2 position = (Vector2)transform.position + move * 3.0f * Time.deltaTime;3. Save your changes and then test the adjustment inside the Unity Editor.
The character will be much faster!
4. In your IDE, uncomment the instructions that you added to the Start function (remove the two slashes), then save and test your changes again.
The illusion of movement is a bit broken because you don’t have enough frames rendered; a key film standard for rendering is 24 frames per second, and the game is running at 10 fps here. The character moves the same number of units per second as when the game ran faster. The character will run at the same speed on any machine, though it may look choppier on slow machines.
5. Return to your script and delete those two lines from the Start function.
Your character now runs at the same speed, regardless of the number of frames rendered by the game — it’s frame independent!
6. Check your script
If you’re completing the 2D Beginner course, take a moment to check that your script is correct before continuing.
PlayerController.cs
Important: If you completed any extension work in your script, this will not be reflected in the reference script below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public InputAction MoveAction;
// Start is called before the first frame update
void Start()
{
MoveAction.Enable();
}
// Update is called once per frame
void Update()
{
Vector2 move = MoveAction.ReadValue<Vector2>();
Debug.Log(move);
Vector2 position = (Vector2)transform.position + move * 3.0f * Time.deltaTime;
transform.position = position;
}
}
7. Next steps
You’ve now made your game frame rate independent, which will improve the experience of players using older hardware.
If you’re completing the 2D Beginner: Adventure Game course, you’ve also completed the first unit. In the next unit, you’ll build on your work on the player character by creating an engaging environment for them to explore and setting up the physics for your game.