Unity での 2D コイン収集

コインのピッキングと収集は 2D ゲーム、特に 2D プラットフォーマー では定番となっています。

Unity でコインを拾うには、coin オブジェクトにアタッチされ、プレイヤーが接触すると破壊されるスクリプトを作成する必要があります。カウンタ値を更新しています。

Sharp Coder ビデオプレーヤー

2D キャラクター コントローラー を使用しますが、既に 2D コントローラーをお持ちの場合は、この部分をスキップできます。

ステップ

拾って収集できる 2D コインを作成するには、次の手順に従います。

  • 新しいゲームオブジェクトを作成し (GameObject -> Create Empty)、名前を付けます。 "Coin"
  • SpriteRenderer コンポーネントを "Coin" オブジェクトにアタッチします
  • コイン スプライトを SpriteRenderer に割り当てます (下の画像を使用できます。インポート設定のテクスチャ タイプが 'Sprite (2D and UI)' に設定されていることを確認してください)。

金貨の透明画像

  • Coin オブジェクトを希望のサイズになるまで拡大縮小します。
  • Coin 'Z' 軸の位置をプレイヤーの位置と一致するように変更します。
  • BoxCollider2D コンポーネントを "Coin" オブジェクトにアタッチします
  • という新しいスクリプトを作成し、"SC_2DCoin" という名前を付け、そこからすべてを削除して、その中に以下のコードを貼り付けます。

SC_2DCoin.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SC_2DCoin : MonoBehaviour
{
    //Keep track of total picked coins (Since the value is static, it can be accessed at "SC_2DCoin.totalCoins" from any script)
    public static int totalCoins = 0; 

    void Awake()
    {
        //Make Collider2D as trigger 
        GetComponent<Collider2D>().isTrigger = true;
    }

    void OnTriggerEnter2D(Collider2D c2d)
    {
        //Destroy the coin if Object tagged Player comes in contact with it
        if (c2d.CompareTag("Player"))
        {
            //Add coin to counter
            totalCoins++;
            //Test: Print total number of coins
            Debug.Log("You currently have " + SC_2DCoin.totalCoins + " Coins.");
            //Destroy coin
            Destroy(gameObject);
        }
    }
}
  • SC_2DCoin スクリプトを "Coin" オブジェクトにアタッチします
  • プレーヤー オブジェクトを選択し、そのタグが "Player" に設定されていることを確認します (これはコインを拾うために必要です)

コインの準備ができたので、Prefab に保存し、レベル全体に複製できます。

コインカウンターを作成するには、以下の手順に従います。

  • [階層] ビューを右クリックして新しい UI イメージを作成し、[UI] -> [イメージ] をクリックして名前を付けます。 "CoinIcon"
  • 画像コンポーネントにコインスプライトを割り当てる
  • RectTransform の配置を 'top left' に、ピボットを (0, 1) に、Post X を '5' に、Pos Y を '-5' に、幅と高さを に変更します。 '25'

  • 階層ビューを右クリックして新しいテキストを作成し、[UI] -> [テキスト] をクリックして名前を付けます。 "CoinCounter"
  • "CoinCounter" RectTransform を "CoinIcon" と同じに設定します。ただし、Pos X を '35' に設定し、Width を '35' に設定します。 '160'
  • テキストのフォント スタイルを 'Bold' に、フォント サイズを 22 に、配置を 'left center' に、色を に設定します。 'white'

  • 新しいスクリプトを作成し、"SC_CoinCounter" という名前を付け、そこからすべてを削除して、その中に以下のコードを貼り付けます。

コイン カウンター スクリプトは、コインの数を Text 要素に適用します。

SC_コインカウンター.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SC_CoinCounter : MonoBehaviour
{
    Text counterText;

    // Start is called before the first frame update
    void Start()
    {
        counterText = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        //Set the current number of coins to display
        if(counterText.text != SC_2DCoin.totalCoins.ToString())
        {
            counterText.text = SC_2DCoin.totalCoins.ToString();
        }
    }
}
  • SC_CoinCounter スクリプトを "CoinCounter" Text オブジェクトにアタッチします

[Play] を押して、プレイヤーが接触するとコインが消え、カウンターに追加されるのを観察します。

おすすめの記事
Unity で新しい HDRP 給水システムを使用する方法
ゾーン コントローラー プロ - Unity アセット ストア パッケージ
FPC Swimmer - 没入型水環境向けの包括的な Unity アセット
Ultimate Spawner 2.0 - 革新的な資産
Unity 用のマウスの外観スクリプト
Weather Maker - Unity 環境を新たな高みに引き上げる
Unity で Xbox コントローラーを使用する方法