Menu
CoddyTech

충돌하면 달리기 종료

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

challenge icon

챌린지

중급

충돌하면 실행이 멈춰야 합니다.

  1. GameManager 위에 public enum GameState { Playing, GameOver }를 선언하고, 그 안에 public GameState state = GameState.Playing;를 추가하세요.
  2. public void GameOver()는 state를 GameState.GameOver로, speed를 0으로 설정하고 gameOverPanel을 표시합니다.
  3. Runner는 Obstacle 태그가 붙은 트리거에 닿으면 GameManager.Instance.GameOver()를 호출합니다. GameManager, Runner, Spawner는 Playing 상태가 아니면 return하도록 Update()를 시작합니다.

‘플레이’를 누르고 충돌해 보세요. 모든 것이 멈춥니다. 그런 다음 ‘확인’을 누르세요.

직접 해보기

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
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;
    public GameObject gameOverPanel;
    public Button restartButton;
    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";
    }
}

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

직접 연습해 보세요: Unity 플레이그라운드