敵がFireBallを発射する2(コルーチンで実行)
![1563ab12 ecd9 427f 8d1a 804256deee26](https://codegenius.org/uploads/slide/image/9037/1563ab12-ecd9-427f-8d1a-804256deee26.jpeg)
![7da191bb 29d6 4aa5 b665 9c78b9eebe22](https://codegenius.org/uploads/slide/image/9038/7da191bb-29d6-4aa5-b665-9c78b9eebe22.jpeg)
![154e9018 e0fb 4157 97a8 7149f59b16d3](https://codegenius.org/uploads/slide/image/9039/154e9018-e0fb-4157-97a8-7149f59b16d3.jpeg)
コルーチンで発射を実行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// FireBallの発射をコルーチンで実行
public class EnemyShot2 : MonoBehaviour
{
public GameObject shotPrefab;
public GameObject shotPoint;
public float interval;
void Start()
{
StartCoroutine(Shot());
}
private IEnumerator Shot()
{
yield return new WaitForSeconds(2f);
while (true)
{
// 1タームに何回繰り返すか?
for (int i = 0; i < 3; i++)
{
Instantiate(shotPrefab, shotPoint.transform.position, Quaternion.identity);
// 何秒間隔で発射するか?
yield return new WaitForSeconds(interval);
}
// 1タームが終了したら3秒間休む
yield return new WaitForSeconds(3f);
}
}
}
![03a02ad2 eebe 4690 9d93 f5b964d083d2](https://codegenius.org/uploads/slide/image/9040/03a02ad2-eebe-4690-9d93-f5b964d083d2.jpeg)
![960fae02 8434 4d04 8164 ccc379fd3f42](https://codegenius.org/uploads/slide/image/9041/960fae02-8434-4d04-8164-ccc379fd3f42.jpeg)
【2022版】ActionGame2D(全33回)
他のコースを見る![1563ab12 ecd9 427f 8d1a 804256deee26](https://codegenius.org/uploads/slide/image/9037/1563ab12-ecd9-427f-8d1a-804256deee26.jpeg)
![7da191bb 29d6 4aa5 b665 9c78b9eebe22](https://codegenius.org/uploads/slide/image/9038/7da191bb-29d6-4aa5-b665-9c78b9eebe22.jpeg)
![154e9018 e0fb 4157 97a8 7149f59b16d3](https://codegenius.org/uploads/slide/image/9039/154e9018-e0fb-4157-97a8-7149f59b16d3.jpeg)
コルーチンで発射を実行
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// FireBallの発射をコルーチンで実行
public class EnemyShot2 : MonoBehaviour
{
public GameObject shotPrefab;
public GameObject shotPoint;
public float interval;
void Start()
{
StartCoroutine(Shot());
}
private IEnumerator Shot()
{
yield return new WaitForSeconds(2f);
while (true)
{
// 1タームに何回繰り返すか?
for (int i = 0; i < 3; i++)
{
Instantiate(shotPrefab, shotPoint.transform.position, Quaternion.identity);
// 何秒間隔で発射するか?
yield return new WaitForSeconds(interval);
}
// 1タームが終了したら3秒間休む
yield return new WaitForSeconds(3f);
}
}
}
![03a02ad2 eebe 4690 9d93 f5b964d083d2](https://codegenius.org/uploads/slide/image/9040/03a02ad2-eebe-4690-9d93-f5b964d083d2.jpeg)
![960fae02 8434 4d04 8164 ccc379fd3f42](https://codegenius.org/uploads/slide/image/9041/960fae02-8434-4d04-8164-ccc379fd3f42.jpeg)
敵がFireBallを発射する2(コルーチンで実行)