砲弾の残弾数を画面に表示する
画面に残弾数を表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
private float interval = 0.75f;
private float timer = 0;
public int shotCount;
// ★追加
public TextMeshProUGUI shellLabel;
// ★追加
private void Start() // 「S」は大文字を確認!
{
shellLabel.text = "" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
{
shotCount -= 1;
// ★追加
shellLabel.text = "" + shotCount;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
}
【2021版】BattleTank(基礎/全33回)
他のコースを見る画面に残弾数を表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using TMPro;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
public GameObject shellPrefab;
public AudioClip shotSound;
private float interval = 0.75f;
private float timer = 0;
public int shotCount;
// ★追加
public TextMeshProUGUI shellLabel;
// ★追加
private void Start() // 「S」は大文字を確認!
{
shellLabel.text = "" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > interval && shotCount > 0)
{
shotCount -= 1;
// ★追加
shellLabel.text = "" + shotCount;
timer = 0.0f;
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent<Rigidbody>();
shellRb.AddForce(transform.forward * shotSpeed);
Destroy(shell, 3.0f);
AudioSource.PlayClipAtPoint(shotSound, transform.position);
}
}
}
砲弾の残弾数を画面に表示する