(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShotShell : MonoBehaviour
{
public GameObject shellPrefab;
public AudioClip sound;
private int shotMaxCount = 5;
private float shotCount;
public Slider shotSlider;
private bool shotPermit = true;
private void Start()
{
shotCount = shotMaxCount;
shotSlider.maxValue = shotMaxCount;
shotSlider.value = shotCount;
}
void Update()
{
// (ポイント)shotPermitの切り替えで、ショットできるかどうかをコントロール
if (Input.GetMouseButtonDown(0) && shotPermit)
{
// 残弾数をスライダーで表示
shotCount -= 1;
shotSlider.value = shotCount;
if(shotCount < 1)
{
shotPermit = false;
}
GameObject shell = Instantiate(shellPrefab, transform.position, Quaternion.identity);
Rigidbody shellRb = shell.GetComponent();
shellRb.AddForce(transform.forward * 1000);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
Destroy(shell, 5.0f);
}
// shotPermitがfalseになったら作動
if (!shotPermit)
{
// リロードタイムをスライダーで表示
shotCount += 0.02f;
shotSlider.value = shotCount;
if(shotCount > shotMaxCount)
{
shotCount = shotMaxCount;
shotPermit = true;
}
}
}
}
(実行結果)
- 砲弾を発射するごとに、スライダーが減少
- 残弾数が0になるとリロード開始
- スライダーが増加