カウンターを作成する


カウンターの作成
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;
}
}
}
}
}



【2019版】AR_Project(全9回)
1 | Vuforiaの初期設定を行う |
2 | キャラクターをAR鑑賞する |
3 | ★チャレンジ課題 |
4 | ARシューティングゲームの開発 |
5 | キャラを破壊する |
6 | オリジナルのカーソルを作成する |
7 | カウンターを作成する |
8 | ★チャレンジ課題 |
9 | ★チャレンジ課題 |


カウンターの作成
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;
}
}
}
}
}



カウンターを作成する