Consider a Unity script that counts how many times Update and FixedUpdate are called during 1 second of game time. What is the expected output?
using UnityEngine; public class CallCounter : MonoBehaviour { private int updateCount = 0; private int fixedUpdateCount = 0; private float timer = 0f; void Update() { updateCount++; timer += Time.deltaTime; if (timer >= 1f) { Debug.Log($"Update calls: {updateCount}, FixedUpdate calls: {fixedUpdateCount}"); updateCount = 0; fixedUpdateCount = 0; timer = 0f; } } void FixedUpdate() { fixedUpdateCount++; } }
Think about the default frame rate and fixed timestep in Unity.
By default, Unity runs Update roughly once per frame (about 60 FPS) and FixedUpdate at a fixed timestep of 0.02 seconds (50 times per second). So in 1 second, FixedUpdate is called about 50 times and Update about 60 times.
Which scenario is best suited for using FixedUpdate instead of Update in Unity?
Think about what kind of operations need consistent timing.
FixedUpdate runs at fixed time intervals and is synced with the physics engine. It is best for applying physics forces to Rigidbody objects to ensure consistent physics simulation.
What will be the output of the following code snippet if the game runs at 60 FPS?
using UnityEngine; public class TimeTest : MonoBehaviour { private float updateSum = 0f; private float fixedUpdateSum = 0f; private int updateCount = 0; private int fixedUpdateCount = 0; void Update() { updateSum += Time.deltaTime; updateCount++; if (updateCount == 60) { Debug.Log($"Update sum: {updateSum}"); } } void FixedUpdate() { fixedUpdateSum += Time.fixedDeltaTime; fixedUpdateCount++; if (fixedUpdateCount == 50) { Debug.Log($"FixedUpdate sum: {fixedUpdateSum}"); } } }
Consider what Time.deltaTime and Time.fixedDeltaTime represent.
Time.deltaTime is the time between frames, about 1/60 seconds at 60 FPS. Summing it over 60 frames gives about 1 second. Time.fixedDeltaTime is fixed at 0.02 seconds, so summing over 50 FixedUpdate calls also gives about 1 second.
A developer moves a Rigidbody by changing its position inside Update(). The movement looks jittery. Why?
void Update()
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}Think about how Unity physics and Rigidbody work.
Rigidbody physics updates happen in FixedUpdate. Moving Rigidbody by changing transform.position in Update causes physics and rendering to be out of sync, resulting in jittery movement.
In Unity, sometimes FixedUpdate is called more than once during a single frame. Why does this happen?
Think about how Unity keeps physics simulation in sync with real time.
If the frame rate is lower than the fixed timestep rate, Unity calls FixedUpdate multiple times per frame to keep physics simulation consistent and not fall behind real time.