カウンターを作成する


画面に破壊した数を表示する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class PlayerShot : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip effectSound;
// ★追加
private int destroyCount;
public Text destroyLabel;
// ★追加
private void Start()
{
destroyLabel.text = "破壊数:" + destroyCount;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject target = hit.collider.gameObject;
if (target.tag == "Chara")
{
Destroy(target.gameObject);
GameObject effect = Instantiate(effectPrefab, target.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(effectSound, Camera.main.transform.position);
// ★追加
destroyCount += 1;
destroyLabel.text = "破壊数:" + destroyCount;
}
}
}
}
}



【2018版】AR_Project(全9回)
1 | AR版ピタゴラスイッチを作る |
2 | キャラクターをAR鑑賞する |
3 | ARシューティングゲームの開発 |
4 | キャラを破壊する |
5 | オリジナルのカーソルを作成する |
6 | カウンターを作成する |
7 | ★チャレンジ課題 |


画面に破壊した数を表示する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class PlayerShot : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip effectSound;
// ★追加
private int destroyCount;
public Text destroyLabel;
// ★追加
private void Start()
{
destroyLabel.text = "破壊数:" + destroyCount;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject target = hit.collider.gameObject;
if (target.tag == "Chara")
{
Destroy(target.gameObject);
GameObject effect = Instantiate(effectPrefab, target.transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(effectSound, Camera.main.transform.position);
// ★追加
destroyCount += 1;
destroyLabel.text = "破壊数:" + destroyCount;
}
}
}
}
}



カウンターを作成する