Menu
CoddyTech

屏幕上的分数

本课属于 Coddy Unity 学习旅程的“打造真正的游戏”单元。第 30 课,共 60 课。

challenge icon

挑战

中等

显示跑者跑了多远。

  1. 在 GameManager 的 Update() 中,速度那一行之后,添加 distance += speed * Time.deltaTime;。
  2. 然后以整米为单位显示距离:distanceText.text = "Distance: " + Mathf.FloorToInt(distance) + " m";。

按下“开始”:随着道路速度加快,米数也会增长得更快。然后按下“检查”。

亲手试试

using UnityEngine;
using TMPro;

public class GameManager : MonoBehaviour
{
    public static GameManager Instance;

    public float speed = 10f;
    public float speedUp = 0.5f;
    public float maxSpeed = 20f;
    public TMP_Text distanceText;
    private float distance;

    void Awake()
    {
        Instance = this;
    }

    void Update()
    {
        speed = Mathf.Min(speed + speedUp * Time.deltaTime, maxSpeed);
    }
}

打造真正的游戏中的所有课

自己练习: Unity 在线编辑器