충돌하면 달리기 종료
Coddy Unity 여정의 실제 게임 만들기 섹션에 포함된 레슨. 60개 중 31번째.
챌린지
중급충돌하면 실행이 멈춰야 합니다.
GameManager위에public enum GameState { Playing, GameOver }를 선언하고, 그 안에public GameState state = GameState.Playing;를 추가하세요.public void GameOver()는state를GameState.GameOver로,speed를 0으로 설정하고gameOverPanel을 표시합니다.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 플레이그라운드