Version: 2021.3
LanguageEnglish
  • C#

MonoBehaviour.FixedUpdate()

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual

Description

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; } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961