Time.deltaTime

Unity · Версия 2020.3 LTS · Scripting · Time

Time.deltaTime

public static float deltaTime;

Описание

Интервал в секундах от последнего кадра до текущего (только для чтения).

При вызове из MonoBehaviour.FixedUpdate возвращается Time.fixedDeltaTime.

deltaTime внутри MonoBehaviour.OnGUI ненадежен, поскольку Unity может вызывать его несколько раз за кадр.

В следующем примере игровой объект вращается вокруг оси Z с постоянной скоростью.

Подробнее о взаимосвязи этого свойства с другими свойствами времени см. в Управление временем и частотой кадров в Руководстве пользователя.

using UnityEngine;
// Rotate around the z axis at a constant speed
public class ConstantRotation : MonoBehaviour
{
    public float degreesPerSecond = 2.0f;
    void Update()
    {
        transform.Rotate(0, 0, degreesPerSecond * Time.deltaTime);
    }
}

The following example implements a timer. The timer adds deltaTime each frame. The example displays the timer value and resets it when it reaches 2 seconds. Time.timeScale controls the speed at which time passes and how fast the timer resets.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Пример Time.deltaTime.
//
// Подождите две секунды и отобразите время ожидания.
// Обычно это чуть больше 2 секунд.
// Позволяет увеличить или уменьшить скорость времени.
// Может варьироваться от 0,5 до 2,0. Только эти изменения
// происходит при перезапуске таймера.

public class ScriptExample : MonoBehaviour
{
    private float waitTime = 2.0f;
    private float timer = 0.0f;
    private float visualTime = 0.0f;
    private int width, height;
    private float value = 10.0f;
    private float scrollBar = 1.0f;

    void Awake()
    {
        width = Screen.width;
        height = Screen.height;
        Time.timeScale = scrollBar;
    }

    void Update()
    {
        timer += Time.deltaTime;

         // Проверить, не вышли ли мы за пределы 2 секунд.
         // Вычитание двух более точно с течением времени, чем сброс до нуля.
        if (timer > waitTime)
        {
            visualTime = timer;

            // Remove the recorded 2 seconds.
            timer = timer - waitTime;
            Time.timeScale = scrollBar;
        }
    }

    void OnGUI()
    {
        GUIStyle sliderDetails = new GUIStyle(GUI.skin.GetStyle("horizontalSlider"));
        GUIStyle sliderThumbDetails = new GUIStyle(GUI.skin.GetStyle("horizontalSliderThumb"));
        GUIStyle labelDetails = new GUIStyle(GUI.skin.GetStyle("label"));

        // Set the size of the fonts and the width/height of the slider.
        labelDetails.fontSize = 6 * (width / 200);
        sliderDetails.fixedHeight = height / 32;
        sliderDetails.fontSize = 12 * (width / 200);
        sliderThumbDetails.fixedHeight = height / 32;
        sliderThumbDetails.fixedWidth = width / 32;

        // Show the slider. Make the scale to be ten times bigger than the needed size.
        value = GUI.HorizontalSlider(new Rect(width / 8, height / 4, width - (4 * width / 8), height - (2 * height / 4)),
            value, 5.0f, 20.0f, sliderDetails, sliderThumbDetails);

        // Show the value from the slider. Make sure that 0.5, 0.6... 1.9, 2.0 are shown.
        float v = ((float)Mathf.RoundToInt(value)) / 10.0f;
        GUI.Label(new Rect(width / 8, height / 3.25f, width - (2 * width / 8), height - (2 * height / 4)),
            "timeScale: " + v.ToString("f1"), labelDetails);
        scrollBar = v;

        // Display the recorded time in a certain size.
        labelDetails.fontSize = 14 * (width / 200);
        GUI.Label(new Rect(width / 8, height / 2, width - (2 * width / 8), height - (2 * height / 4)),
            "Timer value is: " + visualTime.ToString("f4") + " seconds.", labelDetails);
    }
}

Обновлено 17 ноября 2026 г.