Unityでミニゲーム「玉転がし」を作成。スコア(拾ったアイテム数)を表示【Roll-a-Ball.05】

前回、収集品となるアイテムを追加しました。今回は拾ったアイテム数を画面上に表示するいわゆる「スコア」的なのを表示していきます。

スコアの表示

画面にスコアを表示します。今回は画面の左上に拾ったアイテム数をカウントするものを追加していきます。

テキスト枠を追加

まずはUIからTextを追加。

インポートをします。

下図のようにCanvasの配下にTextが出来ていればOK

名前を「CountText」に変更。位置をXを10、Yを-10に。アンカープリセット(表示位置)を左上に変更するので、ShiftとAltを押しながら下図の左上のアイコンを選択。

これで画面の左上に表示する枠が出来ました。下図のような感じですね。

スクリプトの変更

次にスクリプトを変更していきます。「PlayerController」を開いて下のように変更します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;

public class PlayerController : MonoBehaviour
{
    public float speed = 0;
    public TextMeshProUGUI countText;

    private Rigidbody rb;
    private int count;
    private float movementX;
    private float movementY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;

        SetCountText();
    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x;
        movementY = movementVector.y;

    }

    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement * speed);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("PickUp"))
        {
            other.gameObject.SetActive(false);
            count = count + 1;

            SetCountText();
        }
    }
}

追加した内容を簡単に言うと「アイテムを拾ったら+1カウントする」、「カウント数を変数に入れる」です。

変更が終わったらPlayer Controllerの変数(Count Text)にCountTextオブジェクトを割り当てます。

最後にEventSystemを開いて「Replace with InputSystemUIInputModule」を選択。

再生してみる

実際に再生してみます。アイテムを拾ったときに左上のCount数が増えればOK

終了メッセージを表示

最後に、全てのアイテムを拾った時に終了メッセージが表示されるようにします。Canvasの配下にTextを追加。下図のように表示位置、表示内容、フォントサイズなどを設定します。

PlayerControllerスクリプトを変更

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;

public class PlayerController : MonoBehaviour
{
    public float speed = 0;
    public TextMeshProUGUI countText;
    public GameObject winTextObject;

    private Rigidbody rb;
    private int count;
    private float movementX;
    private float movementY;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;

        SetCountText();
        winTextObject.SetActive(false);
    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x;
        movementY = movementVector.y;

    }

    void SetCountText()
    {
        countText.text = "Count: " + count.ToString();
        if (count >= 12)
        {
            winTextObject.SetActive(true);
        }
    }

    private void FixedUpdate()
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY);

        rb.AddForce(movement * speed);
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("PickUp"))



        {
            other.gameObject.SetActive(false);
            count = count + 1;

            SetCountText();
        }

    }
}

追加したのはカウントが「12以上の時にテキストを表示する」です。

CountText同じようにWinTextを割り当てます。

設定が終わったら「再生」します。アイテムを全部拾ってた時に画面に「You Win!」と表示されればOK

タイトルとURLをコピーしました