Frame-rate independent MonoBehaviour.FixedUpdate message for physics calculations.
MonoBehaviour.FixedUpdate has the frequency of the physics system; it is called every fixed frame-rate frame. Compute Physics system calculations after FixedUpdate. 0.02 seconds (50 calls per second) is the default time between calls. Use Time.fixedDeltaTime to access this value. Alter it by setting it to your preferred value within a script, or, navigate to Edit > Settings > Time > Fixed Timestep
and set it there. The FixedUpdate frequency is more or less than Update. If the application runs at 25 frames per second (fps), Unity calls it approximately twice per frame, Alternatively, 100 fps causes approximately two rendering frames with one FixedUpdate. Control the required frame rate and Fixed Timestep
rate from Time
settings. Use Application.targetFrameRate to set the frame rate.
Use FixedUpdate when using Rigidbody. Set a force to a Rigidbody and it applies each fixed frame. FixedUpdate occurs at a measured time step that typically does not coincide with MonoBehaviour.Update.
In the following example, the number of Update calls is compared against the number of FixedUpdate calls. FixedUpdate executes 50 times per second. (The game code runs around 200 fps on a test machine.)
using System.Collections; using System.Collections.Generic; using UnityEngine;
// GameObject.FixedUpdate example. // // Measure frame rate comparing FixedUpdate against Update. // Show the rates every second.
public class ExampleScript : MonoBehaviour { private float updateCount = 0; private float fixedUpdateCount = 0; private float updateUpdateCountPerSecond; private float updateFixedUpdateCountPerSecond;
void Awake() { // Uncommenting this will cause framerate to drop to 10 frames per second. // This will mean that FixedUpdate is called more often than Update. //Application.targetFrameRate = 10; StartCoroutine(Loop()); }
// Increase the number of calls to Update. void Update() { updateCount += 1; }
// Increase the number of calls to FixedUpdate. void FixedUpdate() { fixedUpdateCount += 1; }
// Show the number of calls to both messages. void OnGUI() { GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle("label")); fontSize.fontSize = 24; GUI.Label(new Rect(100, 100, 200, 50), "Update: " + updateUpdateCountPerSecond.ToString(), fontSize); GUI.Label(new Rect(100, 150, 200, 50), "FixedUpdate: " + updateFixedUpdateCountPerSecond.ToString(), fontSize); }
// Update both CountsPerSecond values every second. IEnumerator Loop() { while (true) { yield return new WaitForSeconds(1); updateUpdateCountPerSecond = updateCount; updateFixedUpdateCountPerSecond = fixedUpdateCount;
updateCount = 0; fixedUpdateCount = 0; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.