ステージの拡張4(敵を巡回させる)
![Ee531829 b998 4d10 af23 4c5bd0df009f](https://codegenius.org/uploads/slide/image/8421/ee531829-b998-4d10-af23-4c5bd0df009f.jpeg)
![331207a3 4340 441b ac6a 5a6365c7d5ab](https://codegenius.org/uploads/slide/image/8422/331207a3-4340-441b-ac6a-5a6365c7d5ab.jpeg)
ポイントを巡回移動する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol : MonoBehaviour
{
public Transform[] targets;
public float speed;
private int num = 0;
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
// ゲーム開始時は自動的にスタート地点に配置
transform.position = targets[num].transform.position;
}
void Update()
{
// 指定されたポイントに向きを変える
transform.LookAt(targets[num].transform);
// ベクトルの計算
moveDirection = targets[num].transform.position - transform.position;
controller.SimpleMove(moveDirection * speed);
// 目的地に近づいたら、次のポイントに切り替える。
if(Vector3.Distance(transform.position, targets[num].transform.position) < 1.5f)
{
// 順送りのテクニック
num = (num + 1) % targets.Length;
}
}
}
![A1c17164 84c1 46ed b8c0 0344272faea5](https://codegenius.org/uploads/slide/image/8423/a1c17164-84c1-46ed-b8c0-0344272faea5.jpeg)
![Fb7b8232 7ccd 4fe1 98d0 a368be5f6ed5](https://codegenius.org/uploads/slide/image/8424/fb7b8232-7ccd-4fe1-98d0-a368be5f6ed5.jpeg)
![110fe1e6 3abe 460f b268 cc24b6454dc4](https://codegenius.org/uploads/slide/image/8425/110fe1e6-3abe-460f-b268-cc24b6454dc4.jpeg)
![Efe331d7 555b 49dc 93d2 b02ba47eb07c](https://codegenius.org/uploads/slide/image/8426/efe331d7-555b-49dc-93d2-b02ba47eb07c.jpeg)
![Fa5af008 b02a 42e9 a35f f928acce22dc](https://codegenius.org/uploads/slide/image/8427/fa5af008-b02a-42e9-a35f-f928acce22dc.jpeg)
【2021版】X_Mission(全34回)
他のコースを見る![Ee531829 b998 4d10 af23 4c5bd0df009f](https://codegenius.org/uploads/slide/image/8421/ee531829-b998-4d10-af23-4c5bd0df009f.jpeg)
![331207a3 4340 441b ac6a 5a6365c7d5ab](https://codegenius.org/uploads/slide/image/8422/331207a3-4340-441b-ac6a-5a6365c7d5ab.jpeg)
ポイントを巡回移動する
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol : MonoBehaviour
{
public Transform[] targets;
public float speed;
private int num = 0;
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
controller = GetComponent<CharacterController>();
// ゲーム開始時は自動的にスタート地点に配置
transform.position = targets[num].transform.position;
}
void Update()
{
// 指定されたポイントに向きを変える
transform.LookAt(targets[num].transform);
// ベクトルの計算
moveDirection = targets[num].transform.position - transform.position;
controller.SimpleMove(moveDirection * speed);
// 目的地に近づいたら、次のポイントに切り替える。
if(Vector3.Distance(transform.position, targets[num].transform.position) < 1.5f)
{
// 順送りのテクニック
num = (num + 1) % targets.Length;
}
}
}
![A1c17164 84c1 46ed b8c0 0344272faea5](https://codegenius.org/uploads/slide/image/8423/a1c17164-84c1-46ed-b8c0-0344272faea5.jpeg)
![Fb7b8232 7ccd 4fe1 98d0 a368be5f6ed5](https://codegenius.org/uploads/slide/image/8424/fb7b8232-7ccd-4fe1-98d0-a368be5f6ed5.jpeg)
![110fe1e6 3abe 460f b268 cc24b6454dc4](https://codegenius.org/uploads/slide/image/8425/110fe1e6-3abe-460f-b268-cc24b6454dc4.jpeg)
![Efe331d7 555b 49dc 93d2 b02ba47eb07c](https://codegenius.org/uploads/slide/image/8426/efe331d7-555b-49dc-93d2-b02ba47eb07c.jpeg)
![Fa5af008 b02a 42e9 a35f f928acce22dc](https://codegenius.org/uploads/slide/image/8427/fa5af008-b02a-42e9-a35f-f928acce22dc.jpeg)
ステージの拡張4(敵を巡回させる)