敵を動かす⑤(8の字移動)



8の字移動
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy8Move : MonoBehaviour
{
private float angle;
private Vector3 pos;
private void Start()
{
pos = transform.position;
}
void Update()
{
// 移動速度に相当
angle += Time.deltaTime * 1.2f;
transform.position = new Vector3(
// X軸の幅
pos.x + Mathf.Sin(angle) * 6,
// Y軸
pos.y + transform.position.y,
// Z軸の幅
pos.z + Mathf.Sin(angle * 2) * 2);
}
}






8の字移動(改良)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy8Move : MonoBehaviour
{
private float angle;
private Vector3 pos;
// ★改良
private float plus;
private void Start()
{
pos = transform.position;
}
void Update()
{
angle += Time.deltaTime * 1.2f;
// ★改良
// Z軸方向への移動速度に相当
plus -= 0.05f;
transform.position = new Vector3(
pos.x + Mathf.Sin(angle) * 6,
pos.y + transform.position.y,
// ★改良
pos.z + plus + Mathf.Sin(angle * 2) * 2);
}
}

【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る


8の字移動
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy8Move : MonoBehaviour
{
private float angle;
private Vector3 pos;
private void Start()
{
pos = transform.position;
}
void Update()
{
// 移動速度に相当
angle += Time.deltaTime * 1.2f;
transform.position = new Vector3(
// X軸の幅
pos.x + Mathf.Sin(angle) * 6,
// Y軸
pos.y + transform.position.y,
// Z軸の幅
pos.z + Mathf.Sin(angle * 2) * 2);
}
}






8の字移動(改良)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy8Move : MonoBehaviour
{
private float angle;
private Vector3 pos;
// ★改良
private float plus;
private void Start()
{
pos = transform.position;
}
void Update()
{
angle += Time.deltaTime * 1.2f;
// ★改良
// Z軸方向への移動速度に相当
plus -= 0.05f;
transform.position = new Vector3(
pos.x + Mathf.Sin(angle) * 6,
pos.y + transform.position.y,
// ★改良
pos.z + plus + Mathf.Sin(angle * 2) * 2);
}
}

敵を動かす⑤(8の字移動)