Menu
CoddyTech

Khiên

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

Vật phẩm tăng sức mạnh thứ hai là khiên: nó chặn được một đòn đánh. Nhân vật chạy sẽ phát sáng màu xanh dương khi có khiên.

if (shield)
{
    shield = false;
    Destroy(other.gameObject);
    return;
}
game.GameOver();

Màu của nhân vật chạy là màu của vật liệu, GetComponent<Renderer>().material.color. Giữ lại màu ban đầu, rồi mỗi khung hình chọn màu xanh dương hoặc màu đó.

challenge icon

Thử thách

Dễ

Một vật phẩm khiên chịu được một lần va chạm.

  1. Runner: thêm public bool shield;, body và color; Start đặt body = GetComponent<Renderer>(); và color = body.material.color;.
  2. Thẻ Shield đặt shield = true; khiên sẽ bị dùng hết nếu va chạm với chướng ngại vật, như trên.
  3. Kết thúc Update bằng body.material.color = shield ? new Color(0.3f, 0.9f, 1f) : color;.
  4. Road.Fill: với else if (roll < 0.11f), hãy đặt shieldPrefab theo cách tương tự.

Nhấn Chơi, nhặt khiên màu xanh rồi va vào tường: lượt chơi vẫn tiếp tục. 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 float magnetTime = 8f;

    public int lane;
    public float magnet;

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

    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();
        }
        else if (other.CompareTag("Coin"))
        {
            game.AddCoin();
            Destroy(other.gameObject);
        }
        else if (other.CompareTag("Magnet"))
        {
            magnet = magnetTime;
            Destroy(other.gameObject);
        }
    }
}

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