動く敵の作成③(コルーチンとfor文の組み合わせ)


コルーチンを繰り返す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveEnemyB : 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()
{
// ★for文の活用
// 下記の処理を3回繰り返す
for(int i =0; i<3;i++)
{
// 処理①
x = 1;
z = -1;
speed = 5;
// 中断
yield return new WaitForSeconds(1f);
// 処理②
x = -1;
z = -1;
speed = 5;
// 中断
// その後、処理①に戻る
yield return new WaitForSeconds(1f);
}
}
}




【2021版】Danmaku(基礎/全55回)
他のコースを見る

コルーチンを繰り返す
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveEnemyB : 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()
{
// ★for文の活用
// 下記の処理を3回繰り返す
for(int i =0; i<3;i++)
{
// 処理①
x = 1;
z = -1;
speed = 5;
// 中断
yield return new WaitForSeconds(1f);
// 処理②
x = -1;
z = -1;
speed = 5;
// 中断
// その後、処理①に戻る
yield return new WaitForSeconds(1f);
}
}
}




動く敵の作成③(コルーチンとfor文の組み合わせ)