弾切れの発生④(発射パワーをリセットさせる)
発射パワーのリセット
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;
void Start()
{
shotPower = maxPower;
powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
}
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;
print("全回復までの残り時間" + counter + "秒");
}
shotPower = maxPower;
powerSlider.value = shotPower;
}
// ★追加(発射パワーのリセット)
// プレーヤーが破壊された時、この命令ブロックが呼ばれて実行されるようにする(ポイント)
public void ResetPower()
{
shotPower = maxPower;
powerSlider.value = shotPower;
counter = 0;
}
}
発射パワーのリセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public static int destroyCount = 0;
public bool isMuteki = false;
private ScoreManager scoreManager;
// ★追加(発射パワーのリセット)
private FireMissile fireMissile;
void Start()
{
// ★追加(発射パワーのリセット)
fireMissile = GameObject.Find("FireMissile").GetComponent<FireMissile>();
scoreManager = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
UpdatePlayerIcons();
playerHPSlider = GameObject.Find("PlayerHPSlider").GetComponent<Slider>();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile") && isMuteki == false || other.gameObject.CompareTag("Enemy") && isMuteki == false)
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
playerHPSlider.value = playerHP;
Destroy(other.gameObject);
if (playerHP == 0)
{
destroyCount += 1;
UpdatePlayerIcons();
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
if (destroyCount < 5)
{
Invoke("Retry", 1.0f);
}
else
{
destroyCount = 0;
scoreManager.ScoreReset();
SceneManager.LoadScene("GameOver");
}
}
}
}
void UpdatePlayerIcons()
{
for (int i = 0; i < playerIcons.Length; i++)
{
if (destroyCount <= i)
{
playerIcons[i].SetActive(true);
}
else
{
playerIcons[i].SetActive(false);
}
}
}
void Retry()
{
this.gameObject.SetActive(true);
playerHP = 5;
playerHPSlider.value = playerHP;
isMuteki = true;
Invoke("MutekiOff", 2.0f);
// ★追加(発射パワーのリセット)
fireMissile.ResetPower();
}
void MutekiOff()
{
isMuteki = false;
}
public void AddHP(int amount)
{
playerHP += amount;
if (playerHP > 5)
{
playerHP = 5;
}
playerHPSlider.value = playerHP;
}
public void Player1Up(int amount)
{
destroyCount -= amount;
if (destroyCount < 0)
{
destroyCount = 0;
}
UpdatePlayerIcons();
}
}
発射パワーのリセット
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;
void Start()
{
shotPower = maxPower;
powerSlider = GameObject.Find("PowerSlider").GetComponent<Slider>();
powerSlider.maxValue = maxPower;
powerSlider.value = shotPower;
}
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;
print("全回復までの残り時間" + counter + "秒");
}
shotPower = maxPower;
powerSlider.value = shotPower;
}
// ★追加(発射パワーのリセット)
// プレーヤーが破壊された時、この命令ブロックが呼ばれて実行されるようにする(ポイント)
public void ResetPower()
{
shotPower = maxPower;
powerSlider.value = shotPower;
counter = 0;
}
}
発射パワーのリセット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int playerHP;
private Slider playerHPSlider;
public GameObject[] playerIcons;
public static int destroyCount = 0;
public bool isMuteki = false;
private ScoreManager scoreManager;
// ★追加(発射パワーのリセット)
private FireMissile fireMissile;
void Start()
{
// ★追加(発射パワーのリセット)
fireMissile = GameObject.Find("FireMissile").GetComponent<FireMissile>();
scoreManager = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
UpdatePlayerIcons();
playerHPSlider = GameObject.Find("PlayerHPSlider").GetComponent<Slider>();
playerHPSlider.maxValue = playerHP;
playerHPSlider.value = playerHP;
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("EnemyMissile") && isMuteki == false || other.gameObject.CompareTag("Enemy") && isMuteki == false)
{
playerHP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
playerHPSlider.value = playerHP;
Destroy(other.gameObject);
if (playerHP == 0)
{
destroyCount += 1;
UpdatePlayerIcons();
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
if (destroyCount < 5)
{
Invoke("Retry", 1.0f);
}
else
{
destroyCount = 0;
scoreManager.ScoreReset();
SceneManager.LoadScene("GameOver");
}
}
}
}
void UpdatePlayerIcons()
{
for (int i = 0; i < playerIcons.Length; i++)
{
if (destroyCount <= i)
{
playerIcons[i].SetActive(true);
}
else
{
playerIcons[i].SetActive(false);
}
}
}
void Retry()
{
this.gameObject.SetActive(true);
playerHP = 5;
playerHPSlider.value = playerHP;
isMuteki = true;
Invoke("MutekiOff", 2.0f);
// ★追加(発射パワーのリセット)
fireMissile.ResetPower();
}
void MutekiOff()
{
isMuteki = false;
}
public void AddHP(int amount)
{
playerHP += amount;
if (playerHP > 5)
{
playerHP = 5;
}
playerHPSlider.value = playerHP;
}
public void Player1Up(int amount)
{
destroyCount -= amount;
if (destroyCount < 0)
{
destroyCount = 0;
}
UpdatePlayerIcons();
}
}
弾切れの発生④(発射パワーをリセットさせる)