제목과 다시 실행
Coddy의 Unity 게임: 스카이 레인 코스 레슨. 20개 중 20번째.
마지막으로, 게임에 시작과 끝을 추가합니다. 게임은 제목 화면으로 시작합니다. 제목 화면에는 이름, 최고 점수, 실행할 ‘플레이’ 버튼이 표시됩니다. 점프 키(Space 또는 W)를 눌러 실행을 시작하기 전까지 도로는 움직이지 않고 키를 눌러도 아무 일도 일어나지 않습니다.
over 외에 두 번째 상태인 running이 필요합니다. GameOver가 실행을 끝내고, 그 후 점프 키를 누르면 씬을 다시 불러와 모든 것을 시작할 때 상태로 되돌립니다:
if (over && (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.W)))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}최고 점수는 PlayerPrefs에 저장되므로 다시 불러와도 유지됩니다.
챌린지
중급GameManager에using UnityEngine.SceneManagement;과public bool running;을 추가하세요. Start에서speed = 0f과 제목 메시지를 설정합니다.- Update에서 실행 중이 아니고 게임이 끝난 상태도 아닐 때 Space 또는 W를 누르면 게임을 시작합니다(
running = true, 빈 메시지). 게임 오버 후 Space 또는 W를 누르면 씬을 다시 불러옵니다. 실행 중이 아니면 return합니다. - GameOver는 이제 실행 중이 아니면 반환하고,
running = false를 설정하며, 메시지 끝에"\nJump to run again"을 표시합니다. Runner에서는game.running일 때만 키 입력과 트리거를 확인합니다.
직접 해보기
using UnityEngine;
public class Runner : MonoBehaviour
{
public float laneWidth = 2.5f;
public float laneSpeed = 16f;
public float jumpHeight = 1.6f;
public float gravity = -40f;
public float slideTime = 0.7f;
public float magnetTime = 8f;
public int lane;
public float magnet;
public bool shield;
private CharacterController controller;
private GameManager game;
private Renderer body;
private float fallSpeed;
private float slide;
private Color color;
void Start()
{
controller = GetComponent<CharacterController>();
game = FindFirstObjectByType<GameManager>();
body = GetComponent<Renderer>();
color = body.material.color;
}
void Update()
{
if (!game.over)
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
if (jump && controller.isGrounded)
{
fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
StopSlide();
}
if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
{
slide = slideTime;
Resize(0.5f);
}
}
if (slide > 0f)
{
slide -= Time.deltaTime;
if (slide <= 0f) StopSlide();
}
if (magnet > 0f) magnet -= Time.deltaTime;
if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
fallSpeed += gravity * Time.deltaTime;
float targetX = lane * laneWidth;
float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
controller.Move(move);
body.material.color = shield ? new Color(0.3f, 0.9f, 1f) : color;
ShowPower();
}
void StopSlide()
{
if (transform.localScale.y < 1f) Resize(1f);
slide = 0f;
}
void Resize(float height)
{
controller.enabled = false;
transform.position += Vector3.up * (height - transform.localScale.y);
transform.localScale = new Vector3(1f, height, 1f);
controller.enabled = true;
}
void ShowPower()
{
string text = "";
if (magnet > 0f) text = "Magnet " + Mathf.CeilToInt(magnet);
if (shield) text += (text == "" ? "" : " ") + "Shield";
game.powerText.text = text == "" ? " " : text;
}
void OnTriggerEnter(Collider other)
{
if (game.over) return;
if (other.CompareTag("Obstacle"))
{
if (shield)
{
shield = false;
Destroy(other.gameObject);
return;
}
game.GameOver();
}
else if (other.CompareTag("Coin"))
{
game.AddCoin();
Destroy(other.gameObject);
}
else if (other.CompareTag("Magnet"))
{
magnet = magnetTime;
Destroy(other.gameObject);
}
else if (other.CompareTag("Shield"))
{
shield = true;
Destroy(other.gameObject);
}
}
}
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
Unity 게임: 스카이 레인의 모든 레슨
직접 연습해 보세요: Unity 플레이그라운드