using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy : MonoBehaviour
{
public float enemySpeed;//걸어가는 속도
public Transform target;//현재 타겟
public Transform[] waypoint;// 순찰지점들
public int wayNumber;//현재 순찰 지점
void Update()
{
Vector2 targetV = new Vector2(target.position.x, target.position.y);
transform.position = Vector2.MoveTowards(transform.position, targetV, enemySpeed * Time.deltaTime);
if (target.position.x <= transform.position.x)//얼굴뒤집기
{
this.transform.eulerAngles = new Vector3(0, 180, 0);//향하는 방향따라 뒤집어 주기
}
else
{
this.transform.eulerAngles = new Vector3(0, 0, 0);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "turnpoint")
{
if (target = waypoint[wayNumber])//turnpoint도착하면 다음 위치로.
{
wayNumber++;
wayNumber = wayNumber % 2;//1 2 1 2 반복
target = waypoint[wayNumber];
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy : MonoBehaviour
{
public float enemySpeed;
public Transform target;
public Transform[] waypoint;
public int wayNumber;
public Transform player;
public float dist;
public float checkDist;
void Update()
{
dist = Vector2.Distance(transform.position, player.position);
if (dist > checkDist)
{
target = waypoint[wayNumber];
}
else
{
target = player;
}
Vector2 targetV = new Vector2(target.position.x, target.position.y);
transform.position = Vector2.MoveTowards(transform.position, targetV, enemySpeed * Time.deltaTime);
if (target.position.x <= transform.position.x)//얼굴뒤집기
{
this.transform.eulerAngles = new Vector3(0, 180, 0);
}
else
{
this.transform.eulerAngles = new Vector3(0, 0, 0);
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == "turnpoint")
{
Debug.Log("!!");
if (target = waypoint[wayNumber])
{
wayNumber++;
wayNumber = wayNumber % 2;
target = waypoint[wayNumber];
}
}
}
}