砲弾の残弾数を画面に表示する
砲弾の残弾数を画面に表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
[SerializeField]
private GameObject shellPrefab = null;
[SerializeField]
private AudioClip shotSound = null;
private float timeBetweenShot = 0.75f;
private float timer;
public int shotCount;
// ★追加
[SerializeField]
private Text shellLabel;
// ★追加
// Startの「S」は大文字なので注意!
void Start()
{
shellLabel.text = "砲弾:" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot && 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);
}
}
}
【2020版】BattleTank(基礎/全35回)
他のコースを見る砲弾の残弾数を画面に表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public float shotSpeed;
[SerializeField]
private GameObject shellPrefab = null;
[SerializeField]
private AudioClip shotSound = null;
private float timeBetweenShot = 0.75f;
private float timer;
public int shotCount;
// ★追加
[SerializeField]
private Text shellLabel;
// ★追加
// Startの「S」は大文字なので注意!
void Start()
{
shellLabel.text = "砲弾:" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot && 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);
}
}
}
砲弾の残弾数を画面に表示する