본문 바로가기
유니티강좌

시간 종료시 텍스트 노출. 게임종료(Game Over), 타임어택(Time Over)등에 활용 | 유니티 게임 개발 튜토리얼(Unity & C# Script)

by Ncube 2021. 1. 3.

 

 

정해진 시간(타임바 TimeBar)이 점점 줄어들다가 시간이 종료되면 "GAME OVER" 텍스트가 나타나도록 합니다. 타임어택등의 게임에 활용하면 될 듯하네요. | Image Type(Filled), Fill Method(Horizontal) 

 

#시간종료시텍스트노출  #타임어택  #GameOver

 

 

 

 

[GameOver.cs]

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

public class GameOver : MonoBehaviour
{
    [SerializeField] GameObject gameOverText;
    [SerializeField] float maxTime = 5f;
    float timeLeft;
    Image timerBar;

    // Start is called before the first frame update
    void Start()
    {
        gameOverText.SetActive(false);
        timerBar = GetComponent<Image>();
        timeLeft = maxTime;
    }

    // Update is called once per frame
    void Update()
    {
        if(timeLeft > 0)
        {
            timeLeft -= Time.deltaTime;
            timerBar.fillAmount = timeLeft / maxTime;
        }
        else
        {
            gameOverText.SetActive(true);
            Time.timeScale = 0;
        }
    }
}

 

댓글