ボスの機能の作成⑥(シールド)
ボス・シールド
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossShield : MonoBehaviour
{
public AudioClip sound;
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent(out Missile missile))
{
Destroy(missile.gameObject);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}
シールドの展開と解除
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;
public GameObject darkOptionPrefab;
// ★追加(シールド)
public GameObject bossShield;
private void Start()
{
StartCoroutine(ChangeColor());
// ★追加(シールド)
bossShield.SetActive(false);
}
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];
// ★追加(シールド)
if(num==1)
{
// シールド展開
bossShield.SetActive(true);
}
if (num == 2)
{
// ★追加(シールド)
// シールド解除
bossShield.SetActive(false);
for (int i = 0; i < 4; i++)
{
GameObject darkOption = Instantiate(darkOptionPrefab, transform.position, Quaternion.identity);
Rigidbody darkOptionRb = darkOption.GetComponent<Rigidbody>();
darkOptionRb.AddForce(new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)) * 1000);
yield return new WaitForSeconds(0.3f);
darkOptionRb.velocity = Vector3.zero;
Destroy(darkOption, 10.0f);
}
}
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;
public class BossShield : MonoBehaviour
{
public AudioClip sound;
private void OnTriggerEnter(Collider other)
{
if(other.TryGetComponent(out Missile missile))
{
Destroy(missile.gameObject);
AudioSource.PlayClipAtPoint(sound, transform.position);
}
}
}
シールドの展開と解除
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;
public GameObject darkOptionPrefab;
// ★追加(シールド)
public GameObject bossShield;
private void Start()
{
StartCoroutine(ChangeColor());
// ★追加(シールド)
bossShield.SetActive(false);
}
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];
// ★追加(シールド)
if(num==1)
{
// シールド展開
bossShield.SetActive(true);
}
if (num == 2)
{
// ★追加(シールド)
// シールド解除
bossShield.SetActive(false);
for (int i = 0; i < 4; i++)
{
GameObject darkOption = Instantiate(darkOptionPrefab, transform.position, Quaternion.identity);
Rigidbody darkOptionRb = darkOption.GetComponent<Rigidbody>();
darkOptionRb.AddForce(new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)) * 1000);
yield return new WaitForSeconds(0.3f);
darkOptionRb.velocity = Vector3.zero;
Destroy(darkOption, 10.0f);
}
}
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);
}
}
}
}
}
ボスの機能の作成⑥(シールド)