画面にスコアを表示
CoddyのUnityジャーニー「本格的なゲームを作る」セクションの一部。レッスン 30/60。
チャレンジ
中級ランナーがどれだけ進んだかを表示しましょう。
GameManagerのUpdate()で、速度を更新する行の後にdistance += speed * Time.deltaTime;を追加します。- 次に、メートル単位の整数で表示します:
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プレイグラウンド