カウンターを作成する
![1377bfd5 2ee9 4fad 9adc f551dd4695a2](https://codegenius.org/uploads/slide/image/4426/1377bfd5-2ee9-4fad-9adc-f551dd4695a2.jpeg)
![627acc3e 4e0f 4f7d aff0 814f5c88fdf8](https://codegenius.org/uploads/slide/image/4427/627acc3e-4e0f-4f7d-aff0-814f5c88fdf8.jpeg)
カウンターの作成
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;
}
}
}
}
}
![C9fd778e b922 446b 92e6 32b976ad69a7](https://codegenius.org/uploads/slide/image/4428/c9fd778e-b922-446b-92e6-32b976ad69a7.jpeg)
![2991c3e6 2c9c 4e30 adaa 3ccbc8d5b451](https://codegenius.org/uploads/slide/image/4429/2991c3e6-2c9c-4e30-adaa-3ccbc8d5b451.jpeg)
![20e72721 6d68 40b1 b631 76f2efea8e1f](https://codegenius.org/uploads/slide/image/4430/20e72721-6d68-40b1-b631-76f2efea8e1f.jpeg)
【2019版】AR_Project(全9回)
1 | Vuforiaの初期設定を行う |
2 | キャラクターをAR鑑賞する |
3 | ★チャレンジ課題 |
4 | ARシューティングゲームの開発 |
5 | キャラを破壊する |
6 | オリジナルのカーソルを作成する |
7 | カウンターを作成する |
8 | ★チャレンジ課題 |
9 | ★チャレンジ課題 |
![1377bfd5 2ee9 4fad 9adc f551dd4695a2](https://codegenius.org/uploads/slide/image/4426/1377bfd5-2ee9-4fad-9adc-f551dd4695a2.jpeg)
![627acc3e 4e0f 4f7d aff0 814f5c88fdf8](https://codegenius.org/uploads/slide/image/4427/627acc3e-4e0f-4f7d-aff0-814f5c88fdf8.jpeg)
カウンターの作成
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;
}
}
}
}
}
![C9fd778e b922 446b 92e6 32b976ad69a7](https://codegenius.org/uploads/slide/image/4428/c9fd778e-b922-446b-92e6-32b976ad69a7.jpeg)
![2991c3e6 2c9c 4e30 adaa 3ccbc8d5b451](https://codegenius.org/uploads/slide/image/4429/2991c3e6-2c9c-4e30-adaa-3ccbc8d5b451.jpeg)
![20e72721 6d68 40b1 b631 76f2efea8e1f](https://codegenius.org/uploads/slide/image/4430/20e72721-6d68-40b1-b631-76f2efea8e1f.jpeg)
カウンターを作成する