프로젝트 하나 만듬

Sphere랑 Plane을 하나씩 생성

GameObject 생성

GameObject의 이름을 TimerManager로 바꿈

Add Component 에서 TimerManager.cs를 생성

더블 클릭하여 TimeManager 스크립트를 실행

하기전에 GameObject하나 더 생성

GameObject를 RespawnPosition으로 이름을 바꿈

 Plane 위쪽으로 Position 값을 준다. 

 

Sphere 오브젝트를 프리펩스로 만듬.

바닥으로 떨어트리기 위해 Rigidbody 컴포넌트를 달아주고 Drag 값을 0.5로 바꿈 (공기 저항 값을 줘 살살 내려오게)

코루틴도 멈추는지 테스트 하기 위해 UGUI Text 생성

 

TimeManager.cs의 스크립트를 집어넣음

TimeManager.cs의 코드

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

public class TimeManager : MonoBehaviour
{
    public GameObject respawnPosotion;
    public GameObject sphereObj;

    public float slowdownFactor = 0.05f;
    public float slowdownLength = 2f;

    public Text countText;

    public void DoSlowMotion()
    {
        Time.timeScale = slowdownFactor;
        Time.fixedDeltaTime = Time.timeScale * .02f;
    }

    // Use this for initialization
    void Start()
    {
        StartCoroutine(ITimer());
    }

    private void KeyController()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            DoSlowMotion();
        }
        if (Input.GetButtonDown("Fire2"))
        {
            Time.timeScale = 1f;
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(sphereObj, respawnPosotion.transform.position, respawnPosotion.transform.rotation);
        }
    }

    // Update is called once per frame
    void Update()
    {

        KeyController();

        // 점점 빨라짐
        //Time.timeScale += (1f / slowdownLength) * Time.unscaledDeltaTime;
        //Time.timeScale = Mathf.Clamp(Time.timeScale, 0f, 1f);
    }

    IEnumerator ITimer()
    {
        int i = 0;
        while (true)
        {
            countText.text = i.ToString();
            yield return new WaitForSeconds(0.1f);
            i++;
        }
    }
}

 

드래그해서 넣어주고 Factor, Length 값을 설정함.

 

실행

 

스페이스바를 누르면 공 생성. 마우스 왼쪽 버튼을 눌러 슬로우모션으로 만듬. 마우스 오른쪽 버튼을 눌러 슬로우 모션을 품.

위의 주석을 해제하면 자연스럽게 슬로우 모션이 풀림.

 

사운드도 슬로우 되는지 Audio source를 넣어봐서 실험해봤는데 사운드는 슬로우가 안됨.

슬로우를 하려면 Pitch 값을 조절해주는 코드를 만들어서 사용해야 됨.

 

 

 

참고 동영상

https://www.youtube.com/watch?v=0VGosgaoTsw&t=604s

+ Recent posts