다시 달리기
Coddy Unity 여정의 실제 게임 만들기 섹션에 포함된 레슨. 60개 중 32번째.
챌린지
중급다시 실행 버튼을 눌러 새로 실행되도록 만드세요.
GameManager에서 씬을 다시 로드하는void Restart()를 작성하세요:SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);.restartButton.onClick.AddListener(Restart);가 포함된void Start()를 추가하세요.
‘플레이’를 누르고, 충돌한 다음 다시 실행을 클릭하세요. 새로 실행됩니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public enum GameState { Playing, GameOver }
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;
public GameState state = GameState.Playing;
private float distance;
void Awake()
{
Instance = this;
}
void Update()
{
if (state != GameState.Playing) return;
speed = Mathf.Min(speed + speedUp * Time.deltaTime, maxSpeed);
distance += speed * Time.deltaTime;
distanceText.text = "Distance: " + Mathf.FloorToInt(distance) + " m";
}
public void GameOver()
{
state = GameState.GameOver;
speed = 0f;
gameOverPanel.SetActive(true);
}
}
실제 게임 만들기의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드