カウンターを作成する


カウンターの作成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class PlayerShot : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// ★追加
private int destroyCount;
public TextMeshProUGUI countLabel;
// ★追加
private void Start()
{
countLabel.text = "" + destroyCount.ToString("D3");
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject target = hit.collider.gameObject;
if (target.CompareTag("Enemy"))
{
Destroy(target.gameObject);
GameObject effect = Instantiate(effectPrefab, hit.point, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
// ★追加
destroyCount += 1;
countLabel.text = "" + destroyCount.ToString("D3");
}
}
}
}
}



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


カウンターの作成
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class PlayerShot : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
// ★追加
private int destroyCount;
public TextMeshProUGUI countLabel;
// ★追加
private void Start()
{
countLabel.text = "" + destroyCount.ToString("D3");
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject target = hit.collider.gameObject;
if (target.CompareTag("Enemy"))
{
Destroy(target.gameObject);
GameObject effect = Instantiate(effectPrefab, hit.point, Quaternion.identity);
Destroy(effect, 0.5f);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
// ★追加
destroyCount += 1;
countLabel.text = "" + destroyCount.ToString("D3");
}
}
}
}
}



カウンターを作成する