ボスの機能の作成②(ビーム砲)
ビーム砲に被弾した場合の処理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int maxHP;
private int HP;
public Slider HPSlider;
public int playerCount;
public TextMeshProUGUI playerLabel;
void Start()
{
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
playerLabel.text = "" + playerCount;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
HPSlider.value = HP;
// ★改良
HPZero();
}
// ★追加
if(other.CompareTag("EnemyBeam"))
{
HP = 0;
HPSlider.value = HP;
HPZero();
}
}
void Retry()
{
this.gameObject.SetActive(true);
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
GetComponent<PlayerMovement>().ResetMoveSpeed();
GetComponentInChildren<FireMissile>().ResetShot();
}
// ★改良
// 何度も使う処理はメソッド化すると便利(テクニック)
// コードをこの中に移動させる。
void HPZero()
{
if (HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
playerCount -= 1;
playerLabel.text = "" + playerCount;
GetComponent<OptionController>().ResetOption();
if (playerCount > 0)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
ビーム砲の発射
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossCore : MonoBehaviour
{
public Material[] materials;
private int num = 0;
// ★追加(ビーム砲)
public GameObject beamPrefab;
public GameObject target;
private void Start()
{
StartCoroutine(ChangeColor());
}
// ★追加(ビーム砲)
// 自機狙いの機能
private void Update()
{
transform.LookAt(target.transform);
}
private IEnumerator ChangeColor()
{
while (true)
{
yield return new WaitForSeconds(5f);
num = (num + 1) % materials.Length;
this.gameObject.GetComponent<MeshRenderer>().material = materials[num];
// ★追加(ビーム砲の発射)
// ここでは0.3秒ごとにビーム砲を7個発射する。
if (num == 3)
{
for (int i = 0; i < 7; i++)
{
GameObject beam = Instantiate(beamPrefab, transform.position, transform.rotation);
Rigidbody beamRb = beam.GetComponent<Rigidbody>();
beamRb.AddForce(transform.forward * 1000);
Destroy(beam, 3.0f);
yield return new WaitForSeconds(0.3f);
}
}
}
}
}
【2021版】Danmaku(基礎/全55回)
他のコースを見るビーム砲に被弾した場合の処理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PlayerHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip damageSound;
public AudioClip destroySound;
public int maxHP;
private int HP;
public Slider HPSlider;
public int playerCount;
public TextMeshProUGUI playerLabel;
void Start()
{
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
playerLabel.text = "" + playerCount;
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("EnemyMissile"))
{
HP -= 1;
AudioSource.PlayClipAtPoint(damageSound, Camera.main.transform.position);
Destroy(other.gameObject);
HPSlider.value = HP;
// ★改良
HPZero();
}
// ★追加
if(other.CompareTag("EnemyBeam"))
{
HP = 0;
HPSlider.value = HP;
HPZero();
}
}
void Retry()
{
this.gameObject.SetActive(true);
HP = maxHP;
HPSlider.maxValue = HP;
HPSlider.value = HP;
GetComponent<PlayerMovement>().ResetMoveSpeed();
GetComponentInChildren<FireMissile>().ResetShot();
}
// ★改良
// 何度も使う処理はメソッド化すると便利(テクニック)
// コードをこの中に移動させる。
void HPZero()
{
if (HP == 0)
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 1.0f);
AudioSource.PlayClipAtPoint(destroySound, Camera.main.transform.position);
this.gameObject.SetActive(false);
playerCount -= 1;
playerLabel.text = "" + playerCount;
GetComponent<OptionController>().ResetOption();
if (playerCount > 0)
{
Invoke("Retry", 1.0f);
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
}
ビーム砲の発射
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossCore : MonoBehaviour
{
public Material[] materials;
private int num = 0;
// ★追加(ビーム砲)
public GameObject beamPrefab;
public GameObject target;
private void Start()
{
StartCoroutine(ChangeColor());
}
// ★追加(ビーム砲)
// 自機狙いの機能
private void Update()
{
transform.LookAt(target.transform);
}
private IEnumerator ChangeColor()
{
while (true)
{
yield return new WaitForSeconds(5f);
num = (num + 1) % materials.Length;
this.gameObject.GetComponent<MeshRenderer>().material = materials[num];
// ★追加(ビーム砲の発射)
// ここでは0.3秒ごとにビーム砲を7個発射する。
if (num == 3)
{
for (int i = 0; i < 7; i++)
{
GameObject beam = Instantiate(beamPrefab, transform.position, transform.rotation);
Rigidbody beamRb = beam.GetComponent<Rigidbody>();
beamRb.AddForce(transform.forward * 1000);
Destroy(beam, 3.0f);
yield return new WaitForSeconds(0.3f);
}
}
}
}
}
ボスの機能の作成②(ビーム砲)