Menu
CoddyTech

Bạn đã chạy bao xa

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

Người chạy di chuyển được quãng đường bằng quãng đường con đường dịch chuyển: tốc độ nhân với thời gian, cộng dồn qua mỗi khung hình.

void Update()
{
    distance += speed * Time.deltaTime;
    ShowScore();
}

Cảnh có một dòng chữ ở góc trên bên trái, ScoreText, một dòng chữ TextMeshPro. Kiểu của tập lệnh là TMP_Text (với using TMPro;), còn nội dung hiển thị là scoreText.text. Hiển thị số mét nguyên: (int)distance loại bỏ phần sau dấu thập phân.

challenge icon

Thử thách

Dễ

Đếm quãng đường người chạy đã chạy.

  1. Trong GameManager, thêm using TMPro;, public TMP_Text scoreText; và public float distance;.
  2. Thêm ShowScore(): scoreText.text = ((int)distance).ToString();.
  3. Viết Update như trên và gọi ShowScore() ở cuối Start.

Nhấn Chơi: con số ở góc trên bên trái đếm số mét. 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 float fallSpeed;
    private float slide;

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

    void Update()
    {
        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;
    }
}
quiz iconTự kiểm tra

Bài học này có một bài trắc nghiệm ngắn. Bắt đầu bài học để trả lời và theo dõi tiến độ.

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