Menu
CoddyTech

충돌 후 다시 시작

Coddy Unity 여정의 실제 게임 만들기 섹션에 포함된 레슨. 56개 중 31번째.

challenge icon

챌린지

중급

충돌하면 달리기가 끝나고, 버튼을 누르면 다음 달리기가 시작됩니다. 이전 레슨의 모든 내용을 그대로 유지하세요. 캔버스에는 숨겨진 GameOverPanel이 있으며, RestartButton에는 Run again이라는 레이블이 붙어 있습니다. 씬에서는 GameManager의 새로운 필드 두 개, gameOverPanel과 restartButton을 채웁니다. Runner에는 키네마틱 Rigidbody가 있으므로 장애물로부터 트리거 메시지를 받습니다.

GameManager에서:

  • 클래스 위에 public enum GameState { Playing, GameOver }를 선언하고, 처음에 Playing으로 설정되는 필드 public GameState state를 클래스에 추가하세요.
  • using UnityEngine.UI;, using UnityEngine.SceneManagement;, public GameObject gameOverPanel; 및 public Button restartButton;를 추가하세요.
  • 공개 메서드 GameOver()는 state를 GameOver로 설정하고, 모든 장애물을 멈추도록 speed를 0으로 설정한 다음 패널을 표시합니다.
  • Update()는 상태가 Playing일 때만 동작하므로 속도와 거리가 멈춥니다.
  • Start()에서 restartButton을 누르면 현재 실행 중인 씬이 다시 로드되도록 설정하세요.

Runner에서: Obstacle 태그가 지정된 트리거에 들어가면 GameManager.Instance.GameOver()를 호출하세요. 상태가 Playing이 아니면 화살표 키가 동작하지 않습니다.

Spawner에서: 상태가 Playing이 아니면 아무것도 생성하지 마세요.

Play를 누르고, 충돌한 다음 다시 달리기를 시작하고 Check를 누르세요.

직접 해보기

using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public float speed = 10f;
    public float speedUp = 0.5f;
    public float maxSpeed = 20f;
    public TMP_Text distanceText;
    private float distance;

    void Awake()
    {
        Instance = this;
    }

    void Update()
    {
        speed = Mathf.Min(speed + speedUp * Time.deltaTime, maxSpeed);
        distance += speed * Time.deltaTime;
        distanceText.text = "Distance: " + Mathf.FloorToInt(distance) + " m";
    }
}

실제 게임 만들기의 모든 레슨