(Unity)ワープボタンの実装

(方向性)

  • アイテムを取得すると、「ワープする権利」を獲得
  • ワープボタンを押すと、指定した場所にワープする

(スクリプト)

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

public class Tank_WarpItem : MonoBehaviour
{
    public AudioClip sound;
    public Text warpNumLabel;
    private int warpCount = 0;
    public GameObject warpPoint;

    private void Start()
    {
        warpNumLabel.text = "" + warpCount;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("TankWarpItem"))
        {
            Destroy(other.gameObject);
            
            warpCount += 1;
            warpNumLabel.text = "" + warpCount;
        }
    }

    public void OnWarpButtonClicked()
    {
        if(warpCount > 0)
        {
            transform.position = warpPoint.transform.position;
            AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
            warpCount -= 1;
            warpNumLabel.text = "" + warpCount;
        }
    }
}

(実行結果)

  • アイテムを取得すると、ボタンの数字が増える。

  • ワープボタンを押すと、指定した場所にワープ
  • 数字が1減る