[Unity] Time.deltaTime 왜 쓰는걸까?

ChangJin·2023년 12월 17일
0

Unity

목록 보기
3/17
post-thumbnail

공식 docs를 참고해보면...

https://docs.unity3d.com/ScriptReference/Time-deltaTime.html

게임을 하면서 FPS라는 단어를 들어보셨을 겁니다. Frames Per Second 의 약자이고 초당 프레임 수 라고 합니다.

Time.deltaTime는 마지막 frame에서 현재 frame까지의 시간 간격을 나타냅니다.

Read Only 임을 잊으면 안됩니다. MonoBehaviour.FixedUpdate 안에서 불려졌을 때 Time.deltaTime을 반환합니다. MonoBehaviour.OnGUI 에서는 reliable하지 않습니다. frame마다 여러 번 Time.deltaTime을 호출 할 수도 있기 때문입니다.


using UnityEngine;
// 일정한 속도로 z 축을 기준으로 회전하기
public class ConstantRotation : MonoBehaviour
{
    public float degreesPerSecond = 2.0f;
    void Update()
    {
        transform.Rotate(0, 0, degreesPerSecond * Time.deltaTime);
    }
}

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

// Time.deltaTime example.
//
// 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초가 지났는지 확인함
        // 0으로 초기화하는 것보다 2초가 지난 이후 경과한 시간 만큼을 빼는게 더 정확함
        if (timer > waitTime)
        {
            visualTime = timer;

            // 기록된 2초를 삭제함
            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"));

        // 슬라이더의 폰트사이즈, 가로/세로 높이를 설정함
        labelDetails.fontSize = 6 * (width / 200);
        sliderDetails.fixedHeight = height / 32;
        sliderDetails.fontSize = 12 * (width / 200);
        sliderThumbDetails.fixedHeight = height / 32;
        sliderThumbDetails.fixedWidth = width / 32;

        // 슬라이더를 보여주기. 필요한 사이즈보다 10배 scal하기
        value = GUI.HorizontalSlider(new Rect(width / 8, height / 4, width - (4 * width / 8), height - (2 * height / 4)),
            value, 5.0f, 20.0f, sliderDetails, sliderThumbDetails);
            
        // 슬라이더의 value 보여주기. 0.5, 0.6... 1.9, 2.0 이렇게 보여주기.
        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;

        // 특정 크기에서 기록된 시간을 보여주기
        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);
    }
}
profile
게임 프로그래머

0개의 댓글

관련 채용 정보