弾切れの発生⑤(待機時間を表示する)
http://mono-pro.net/unity-codegenius-danmaku-waittime/画像データ
待機時間の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float missileSpeed;
public AudioClip fireSound;
private int timeCount;
private int maxPower = 100;
private int shotPower;
private Slider powerSlider;
const int RecoveryTime = 10;
private int counter;
// ★追加(発射パワーの表示)
private Slider waitTimeSlider;
void Start()
{
shotPower = maxPower;
powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
// ★追加(発射パワーの表示)
waitTimeSlider = GameObject.Find("WaitTimeSlider").GetComponent<Slider>();
waitTimeSlider.maxValue = RecoveryTime;
waitTimeSlider.value = RecoveryTime;
}
void Update()
{
if (shotPower <= 0 && counter <= 0)
{
StartCoroutine(RecoverPower());
}
timeCount += 1;
if (Input.GetButton("Fire1"))
{
if (shotPower <= 0)
{
return;
}
shotPower -= 1;
powerSlider.value = shotPower;
if (timeCount % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * missileSpeed);
AudioSource.PlayClipAtPoint(fireSound, transform.position);
Destroy(missile, 2.0f);
}
}
}
IEnumerator RecoverPower()
{
counter = RecoveryTime;
while (counter > 0)
{
yield return new WaitForSeconds(1.0f);
counter -= 1;
// ★追加(発射パワーの表示)
waitTimeSlider.value = counter;
print("全回復までの残り時間" + counter + "秒");
}
shotPower = maxPower;
powerSlider.value = shotPower;
// ★追加(発射パワーの表示)
// 待機時間が終了したら待機ゲージを最初の状態に戻す。
waitTimeSlider.value = RecoveryTime;
}
public void ResetPower()
{
shotPower = maxPower;
powerSlider.value = shotPower;
counter = 0;
// ★追加(発射パワーの表示)
// プレーヤーが破壊されたら待機ゲージを最初の状態に戻す。
waitTimeSlider.value = RecoveryTime;
}
}
http://mono-pro.net/unity-codegenius-danmaku-waittime/画像データ
待機時間の表示
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FireMissile : MonoBehaviour
{
public GameObject missilePrefab;
public float missileSpeed;
public AudioClip fireSound;
private int timeCount;
private int maxPower = 100;
private int shotPower;
private Slider powerSlider;
const int RecoveryTime = 10;
private int counter;
// ★追加(発射パワーの表示)
private Slider waitTimeSlider;
void Start()
{
shotPower = maxPower;
powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
// ★追加(発射パワーの表示)
waitTimeSlider = GameObject.Find("WaitTimeSlider").GetComponent<Slider>();
waitTimeSlider.maxValue = RecoveryTime;
waitTimeSlider.value = RecoveryTime;
}
void Update()
{
if (shotPower <= 0 && counter <= 0)
{
StartCoroutine(RecoverPower());
}
timeCount += 1;
if (Input.GetButton("Fire1"))
{
if (shotPower <= 0)
{
return;
}
shotPower -= 1;
powerSlider.value = shotPower;
if (timeCount % 5 == 0)
{
GameObject missile = Instantiate(missilePrefab, transform.position, Quaternion.identity);
Rigidbody missileRb = missile.GetComponent<Rigidbody>();
missileRb.AddForce(transform.forward * missileSpeed);
AudioSource.PlayClipAtPoint(fireSound, transform.position);
Destroy(missile, 2.0f);
}
}
}
IEnumerator RecoverPower()
{
counter = RecoveryTime;
while (counter > 0)
{
yield return new WaitForSeconds(1.0f);
counter -= 1;
// ★追加(発射パワーの表示)
waitTimeSlider.value = counter;
print("全回復までの残り時間" + counter + "秒");
}
shotPower = maxPower;
powerSlider.value = shotPower;
// ★追加(発射パワーの表示)
// 待機時間が終了したら待機ゲージを最初の状態に戻す。
waitTimeSlider.value = RecoveryTime;
}
public void ResetPower()
{
shotPower = maxPower;
powerSlider.value = shotPower;
counter = 0;
// ★追加(発射パワーの表示)
// プレーヤーが破壊されたら待機ゲージを最初の状態に戻す。
waitTimeSlider.value = RecoveryTime;
}
}
弾切れの発生⑤(待機時間を表示する)