타이틀 화면
Coddy의 Unity 게임: 하늘길 코스 레슨. 21개 중 20번째.
게임이 시작 화면을 갖게 됩니다. 게임 이름, 최고 점수, 달리기 시작 안내가 있는 제목 화면이 표시됩니다. 점프 키(스페이스바 또는 W)를 눌러 달리기를 시작할 때까지 길은 멈춰 있고 키를 눌러도 아무 일도 일어나지 않습니다.
over 외에 두 번째 상태인 running이 필요합니다. 처음에는 false이며, 점프하면 true가 되고 GameOver가 다시 false로 설정합니다. 메시지의 \n은 새 줄을 시작합니다.
messageText.text = "My Game\nBest: " + PlayerPrefs.GetInt("BestScore", 0) + "\nPress to start";게임을 움직이는 Update의 모든 코드는 if (!running) return; 뒤에 오므로 제목 화면에서는 아무것도 움직이지 않습니다.
챌린지
중급점프를 기다리는 타이틀 화면에서 게임을 여세요.
GameManager에public bool running;을 추가하세요. Start에서speed = 0f와messageText.text = "Sky Lanes\nBest: " + PlayerPrefs.GetInt("BestScore", 0) + "\nJump to run";을 설정하세요.- Update에서
!running && !over일 때 Space 또는 W를 누르면running = true로 설정하고 메시지를 비우세요. 그런 다음if (!running) return;을 실행하세요. - GameOver는
if (!running) return;과running = false;로 시작하세요. 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 플레이그라운드