動く敵の作成④(コルーチンとランダムの組み合わせ)
![9f602fbe eaec 4976 b07d 0d178d8c66a8](https://codegenius.org/uploads/slide/image/7606/9f602fbe-eaec-4976-b07d-0d178d8c66a8.jpeg)
コルーチン&ランダム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveEnemyC : EnemyBase
{
private float x;
private float z;
private float speed;
void Start()
{
HP = 1;
StartCoroutine(MoveE());
}
void Update()
{
transform.Translate(new Vector3(x, 0, z) * speed * Time.deltaTime, Space.World);
}
private IEnumerator MoveE()
{
x = 0;
z = -1;
speed = 5;
// ランダム要素の追加
// Random.Range(最小値<含む>, 最大値<含まない>)の使い方をマスターしよう!
// 下記は、1秒,2秒,3秒,4秒の間のいずれかにランダムに決定します。
yield return new WaitForSeconds(Random.Range(1, 5));
x = 1;
z = 0;
// speedもランダムに変化させる
speed = Random.Range(1, 11);
}
}
![34489149 47a3 46b3 8529 3e472370e0c4](https://codegenius.org/uploads/slide/image/7607/34489149-47a3-46b3-8529-3e472370e0c4.jpeg)
![85f28c15 43d8 42f4 bca2 b9ccc0604c15](https://codegenius.org/uploads/slide/image/7608/85f28c15-43d8-42f4-bca2-b9ccc0604c15.jpeg)
![Cac92e26 6da3 433e a045 613e47a92bdd](https://codegenius.org/uploads/slide/image/7609/cac92e26-6da3-433e-a045-613e47a92bdd.jpeg)
![E5ccc660 c7d2 44cc 94a8 ebfd6c9f78e5](https://codegenius.org/uploads/slide/image/7610/e5ccc660-c7d2-44cc-94a8-ebfd6c9f78e5.jpeg)
【2021版】Danmaku(基礎/全55回)
他のコースを見る![9f602fbe eaec 4976 b07d 0d178d8c66a8](https://codegenius.org/uploads/slide/image/7606/9f602fbe-eaec-4976-b07d-0d178d8c66a8.jpeg)
コルーチン&ランダム
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveEnemyC : EnemyBase
{
private float x;
private float z;
private float speed;
void Start()
{
HP = 1;
StartCoroutine(MoveE());
}
void Update()
{
transform.Translate(new Vector3(x, 0, z) * speed * Time.deltaTime, Space.World);
}
private IEnumerator MoveE()
{
x = 0;
z = -1;
speed = 5;
// ランダム要素の追加
// Random.Range(最小値<含む>, 最大値<含まない>)の使い方をマスターしよう!
// 下記は、1秒,2秒,3秒,4秒の間のいずれかにランダムに決定します。
yield return new WaitForSeconds(Random.Range(1, 5));
x = 1;
z = 0;
// speedもランダムに変化させる
speed = Random.Range(1, 11);
}
}
![34489149 47a3 46b3 8529 3e472370e0c4](https://codegenius.org/uploads/slide/image/7607/34489149-47a3-46b3-8529-3e472370e0c4.jpeg)
![85f28c15 43d8 42f4 bca2 b9ccc0604c15](https://codegenius.org/uploads/slide/image/7608/85f28c15-43d8-42f4-bca2-b9ccc0604c15.jpeg)
![Cac92e26 6da3 433e a045 613e47a92bdd](https://codegenius.org/uploads/slide/image/7609/cac92e26-6da3-433e-a045-613e47a92bdd.jpeg)
![E5ccc660 c7d2 44cc 94a8 ebfd6c9f78e5](https://codegenius.org/uploads/slide/image/7610/e5ccc660-c7d2-44cc-94a8-ebfd6c9f78e5.jpeg)
動く敵の作成④(コルーチンとランダムの組み合わせ)