using UnityEngine;
using System.Collections;
// 적 캐릭터의 이동을 처리하는 클래스
// 지정한 패스의 순서대로 이동하게 설계
public class EnemyMove_1 : MonoBehaviour {
// 인스펙터 창에서 Empty GameObject를 등록해서 패스를 사용
public GameObject[] Path;
// 이동할 적 캐릭터를 인스펙터 창에서 등록
public GameObject Enemy;
// 적의 이동 속도 조절
public float Speed = 0.0f;
// 현재 가지고 있는 패스
int NowPathNum = 0;
// 캐릭터의 이동 벡터
Vector3 DirectionVector;
// Use this for initialization
void Start () {
// 패스가 0~1개로 등록 됬을 경우 이동을 금지
// 즉 무조건 패스는 2개 이상 등록
if (Path.Length > 1) {
// 적의 시작 위치를 패스의 0번으로 초기화
Enemy.transform.position = Path [NowPathNum].gameObject.transform.position;
// 현재 위치에서 다음 위치까지의 방향벡터 구함
DirectionVector = (Path [NowPathNum+1].gameObject.transform.position - Path [NowPathNum].gameObject.transform.position).normalized;
}
}
// Update is called once per frame
void Update () {
if (Path.Length > 1) {
// 캐릭터 이동
Enemy.transform.position += (DirectionVector * Speed * Time.deltaTime);
// 만약 다음 가야할 패스가 마지막이 아니고
if (Path.Length > NowPathNum + 1) {
// 다음 가야할 위치와 현재 적의 위치 사이의 거리가 일정 범위 이하 일때
if ((Path [NowPathNum + 1].gameObject.transform.position - Enemy.transform.position).magnitude < 1.0f) {
NowPathNum++; // 현재 패스를 다음 패스로 바꿈.
if (Path.Length != NowPathNum) {
// 현재 위치에서 다음 위치까지의 방향벡터 구함
DirectionVector = (Path [NowPathNum + 1].gameObject.transform.position - Path [NowPathNum].gameObject.transform.position).normalized;
}
}
}
else {
// 더 이상 이동할 패스가 없을때 적 캐릭터 삭제
GameObject.Destroy (this.gameObject);
}
}
}
}
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour {
// 랜덤으로 출현 할 적 캐릭터를 담을 배열
public GameObject[] Enemy;
// 몇 초에 한번씩 적 캐릭터를 스폰 시킬 지 시간
public float enemyTime = 2.0f;
// 현재 몇초가 지났는지 저장하는 변수
float NowTime = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// 시간을 계속 더해준다.
NowTime += Time.deltaTime;
// 현재 시간이 지정한 시간보다 넘었을 경우
if(NowTime >= enemyTime){
NowTime = 0.0f;
// 0~Enemy배열의 개수만큼 랜덤으로 숫자를 생성하고
int Random_ID = Random.Range(0, Enemy.Length-1);
// 이에 맞는 적을 생성한다.
Instantiate(Enemy[Random_ID]);
}
}
}