動く敵の作成④(コルーチンとランダムの組み合わせ)

コルーチン&ランダム
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);
}
}




【2021版】Danmaku(基礎/全55回)
他のコースを見る
コルーチン&ランダム
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);
}
}




動く敵の作成④(コルーチンとランダムの組み合わせ)