敵を巡回移動させる



敵を巡回させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Patrol : MonoBehaviour
{
    public Transform[] targets;
    private int num = 0;
    private CharacterController controller;
    private Vector3 moveDirection = Vector3.zero;
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }
    void Update()
    {
        // 指定されたチェックポイントに向きを変える。
        transform.LookAt(targets[num].transform);
        // ベクトルの計算
        moveDirection = targets[num].transform.position - transform.position;
        controller.SimpleMove(moveDirection * 0.3f);
        // 目的地に近づいたら、次のチェックポイントに切り替える。
        if(Vector3.Distance(transform.position, targets[num].transform.position) < 1.5f)
        {
            // 順送りのテクニック
            num = (num + 1) % targets.Length;
        }
    }
}



【2019版】X_Mission(基礎/全51回)
他のコースを見る


敵を巡回させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Patrol : MonoBehaviour
{
    public Transform[] targets;
    private int num = 0;
    private CharacterController controller;
    private Vector3 moveDirection = Vector3.zero;
    void Start()
    {
        controller = GetComponent<CharacterController>();
    }
    void Update()
    {
        // 指定されたチェックポイントに向きを変える。
        transform.LookAt(targets[num].transform);
        // ベクトルの計算
        moveDirection = targets[num].transform.position - transform.position;
        controller.SimpleMove(moveDirection * 0.3f);
        // 目的地に近づいたら、次のチェックポイントに切り替える。
        if(Vector3.Distance(transform.position, targets[num].transform.position) < 1.5f)
        {
            // 順送りのテクニック
            num = (num + 1) % targets.Length;
        }
    }
}



敵を巡回移動させる