Menu
CoddyTech

Nhảy, trượt hoặc né

Bài học 11 trên 21 trong khóa học Trò chơi Unity: đường đua trên trời của Coddy.

Một loại chướng ngại vật tương ứng với một câu trả lời: nhảy. Ba loại thì cần trả lời ba câu hỏi:

Một rào chắn màu đỏ trên đường, y 0.5: nhảy qua; một thanh ngang màu vàng treo ở y 1.95: trượt bên dưới; một bức tường màu tím, y 1.5 và cao ba đơn vị: đổi làn.

Các thanh ngang và bức tường cũng được gắn thẻ Obstacle, nên đoạn xử lý va chạm bạn đã viết sẽ kết thúc lượt chơi khi va vào chúng. Hai phương thức nhỏ chuyển một số loại thành prefab và chiều cao:

GameObject Obstacle(int kind)
{
    if (kind == 0) return barrierPrefab;
    if (kind == 1) return barPrefab;
    return wallPrefab;
}
challenge icon

Thử thách

Dễ

Thêm các thanh để trượt bên dưới và các bức tường để né tránh.

  1. Thêm barPrefab và wallPrefab vào Road.
  2. Viết Obstacle(kind) như trên và Height(kind): 0.5f cho kind 0, 1.95f cho kind 1, 1.5f cho tường.
  3. Trong Fill, chọn int kind = Random.Range(0, 3); và đặt Obstacle(kind) ở độ cao Height(kind).

Nhấn Chơi: các rào chắn, thanh chắn và bức tường lao xuống đường, va phải bất kỳ vật nào trong số đó cũng sẽ kết thúc lượt chơi. Sau đó nhấn Kiểm tra.

Tự làm thử

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 int lane;

    private CharacterController controller;
    private GameManager game;
    private float fallSpeed;
    private float slide;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        game = FindFirstObjectByType<GameManager>();
    }

    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 (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);
    }

    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 OnTriggerEnter(Collider other)
    {
        if (game.over) return;
        if (other.CompareTag("Obstacle"))
        {
            game.GameOver();
        }
    }
}

Tất cả bài học trong Trò chơi Unity: đường đua trên trời

Tự luyện tập: Playground Unity