Menu
CoddyTech

더 빠르게, 더 빠르게

Coddy의 Unity 게임: 스카이 레인 코스 레슨. 20개 중 17번째.

끝없이 달리는 게임은 점점 더 어려워지기 때문에 계속 흥미진진합니다. 달리기가 얼마나 오래 이어졌는지 세고, 최고 속도까지 속도를 높여 보세요.

runTime += Time.deltaTime;
speed = Mathf.Min(maxSpeed, startSpeed + speedUp * runTime);

속도는 10에서 시작해 초당 0.25씩 증가하고, 56초 후에는 24에 도달합니다. 최고 속도는 난이도만을 위한 것이 아닙니다. 초당 24유닛으로 달리면 도로는 프레임당 0.4유닛 이동하는데, 이는 달리는 캐릭터의 너비보다 여전히 작으므로 두 프레임 사이에 장애물을 통과해 버리는 일은 없습니다.

challenge icon

챌린지

쉬움
  1. GameManager에 public float maxSpeed = 24f;, public float speedUp = 0.25f; 및 public float runTime;을 추가하세요.
  2. Update에서 distance를 계산하기 전에 runTime을 증가시키고 위와 같이 속도를 설정하세요.

직접 해보기

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 플레이그라운드