
Make your game frame rate independent
Tutorial
·
Beginner
·
+0XP
·
20 mins
·
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.
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.
1. Overview
The frame rate of a game is how often the image (frame) is updated when that game is displayed (rendered) on a screen. Frame rate is also sometimes called the update rate, and you might have used the Update function included in Unity’s default C# script template — this function is called every time a new frame is rendered.
Frame rate can impact game performance and player experience, so you need to make considered choices regarding your game as a creator.
Note: For the purposes of this tutorial, we chose to use the Ruby’s Adventure asset set, and the images 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 — for example, there’s a significant difference between 20fps and 100fps.
The video below is an example of a game running at a framerate of 20 fps:
What affects 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 Adventure Game: Robot Repair2D Beginner: Adventure Game course is currently linked to the frame rate: the character moves in units per frame. 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 game, follow these instructions:
1. Enter Play mode and test the player character movement speed. By default, this game runs at 60 fps.
Note: Don’t forget to exit Play mode when you’re done testing.
2. Open the PlayerController script.
3. At the beginning of the Start function, add the following lines of code:
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 extremely slow!
Note: 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.
If your game runs at 60 fps, the character will move at a rate of 0.01 * 60, which gives 0.6 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.01 * 10, so 0.1 units 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: 1 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 code 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’ve written.
3. Inside the Update function, find the instruction that defines the position variable. Make the following adjustment so it ends up as follows:
Vector2 position = (Vector2)transform.position + move * 0.01f * Time.deltaTime;Here’s an explanation of your change:
- Time.deltaTime 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.01 units per second.
4. Save your changes (Ctrl/Cmd+S) 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 extremely slow. Exit Play mode when you’re done.
Why is the character so slow?
Although the player character is even slower than before, your code is actually working as expected. The current value to increment (increase) the position is 0.01, so the player character moves 0.01 units per second and takes 100 seconds to move a full unit. Next, you’ll change the code to fix this behaviour.
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 player character now runs at a suitable speed, regardless of the number of frames rendered by the game — it’s frame independent!
Note: You can now remove the commented lines inside the Start function, as they’re no longer needed.
6. Check your script
PlayerController.cs
Important: If you completed any extension work in your script, this will not be reflected in the reference script below.
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public InputAction MoveAction;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
//QualitySettings.vSyncCount = 0;
//Application.targetFrameRate = 10;
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 Adventure Game: Robot Repair 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 set up the physics for your game.