砲弾の残弾数を画面に表示する








残弾数を画面に表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
// ★追加
public Text shellLabel;
private float timeBetweenShot = 0.35f;
private float timer;
// ★追加
// Startの「S」は大文字なので注意!
void Start()
{
shellLabel.text = "砲弾:" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
if (shotCount < 1)
{
return;
}
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);
}
}
}




BattleTank(基礎/全31回)
他のコースを見る







残弾数を画面に表示する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// ★追加
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public float shotSpeed;
public AudioClip shotSound;
public int shotCount;
// ★追加
public Text shellLabel;
private float timeBetweenShot = 0.35f;
private float timer;
// ★追加
// Startの「S」は大文字なので注意!
void Start()
{
shellLabel.text = "砲弾:" + shotCount;
}
void Update()
{
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space) && timer > timeBetweenShot)
{
if (shotCount < 1)
{
return;
}
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);
}
}
}




砲弾の残弾数を画面に表示する