using System.Collections; using UnityEngine; public class Enemy : MonoBehaviour { private int wayPointCount; //이동경로 개수 private Transform[ ] wayPoints; //이동경로 정보 private int currentIndex = 0; //현재 목표지점 인덱스 private Movement2D movement2D; // 오브젝트 이동 제어 public void Setup(Transform[] wayPoints) { movement2D = GetComponent(); //적 이동경로 wayPoints 정보 설정 wayPointCount = wayPoints.Length; this.wayPoints = new Transform[wayPointCount]; this.wayPoints = wayPoints; //적 위치 = 첫 wayPoint 설정 transform.position = wayPoints[currentIndex].position; StartCoroutine("OnMove"); } IEnumerator OnMove() { //다음 이동 NextMoveTo(); while (true) { transform.Rotate(Vector3.forward * 10); if (Vector3.Distance(transform.position, wayPoints[currentIndex].position) < 0.02f * movement2D.MoveSpeed) { NextMoveTo(); } yield return null; } } void NextMoveTo() { //wayPoints 남아있으면 if ( currentIndex < wayPointCount -1 ) { transform.position = wayPoints[currentIndex].position; currentIndex ++; Vector3 direction = (wayPoints[currentIndex].position - transform.position).normalized; movement2D.MoveTo(direction); } //마지막 wayPoints이면 else { Destroy(gameObject); } } }