게임이 시작되면 상단의 시간(Remain Time)이 줄어들기 시작합니다. 폭탄 캐릭터를 움직여서 시간 증가 아이템, 시간 감소 아이템을 먹을 때 마다 시간이 증감하게 됩니다. 상단의 시간이 0이 되면 화면 중앙에 "GAME OVER" 텍스트가 나오고 하단에는 재시작(Restart) 버튼이 노출됩니다. 재시작 버튼을 누르면 게임은 다시 시작됩니다. 시간 제한 게임(타임어택)등에 응용해서 제작하면 유용할 듯 합니다.
collision.gameObject.SetActive(true / false); 로 화면 노출 조절
SceneManager.LoadScene("Scene Name"); 재시작 버튼 클릭시 해당 씬 로드.
[BombMove.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BombMove : MonoBehaviour
{
[SerializeField] float speed = 7f;
float moveX, moveY;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
moveX = Input.GetAxis("Horizontal");
moveY = Input.GetAxis("Vertical");
rb.velocity = new Vector2(moveX * speed, moveY * speed);
}
void OnTriggerEnter2D(Collider2D collision)
{
switch (collision.gameObject.tag)
{
case "Plus2":
RemainTime.rTime += 2f;
collision.gameObject.SetActive(false);
break;
case "Minus3":
RemainTime.rTime -= 3f;
collision.gameObject.SetActive(false);
break;
}
}
}
[RemainTime.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RemainTime : MonoBehaviour
{
Text text;
public static float rTime = 10f;
void Start()
{
text = GetComponent<Text>();
}
void Update()
{
rTime -= Time.deltaTime;
if (rTime < 0)
rTime = 0;
text.text = "Remain Time : " + Mathf.Round(rTime);
}
}
[GameControl.cs]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameControl : MonoBehaviour
{
[SerializeField] GameObject gameOver, restartBtn;
[SerializeField] float playTime = 10f;
void Update()
{
EndTime();
}
void EndTime()
{
if(RemainTime.rTime <= 0)
{
Time.timeScale = 0;
gameOver.gameObject.SetActive(true);
restartBtn.gameObject.SetActive(true);
}
}
public void restartGame()
{
gameOver.gameObject.SetActive(false);
restartBtn.gameObject.SetActive(false);
Time.timeScale = 1;
RemainTime.rTime = playTime;
SceneManager.LoadScene("GetTime");
}
}
댓글