using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Mathematics;
using TMPro;
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;
public float height;//높이측정
//public float wayHeight;//방해물있을시 점프
public Rigidbody2D rigid;//점프용 리지드바디
public float jumpPower = 5.0f;
public bool isground;//값을 받을 bool값
public bool iswall;//장애물 체크
public Transform groundCheck;//player발위치
public Transform wallCheck;//장애물 인식
public float groundRadius = 0.2f;//측정할 범위
public LayerMask whatIsGround;//어떤 layer를 측정할지
public Animator animator;//애니메이터 추가
public TextMeshPro textMeshPro;
void Update()
{
textMeshPro.transform.position = this.transform.position + Vector3.up * 6f;//글자가 머리위에 오게하기
dist = Vector2.Distance(transform.position, player.position);
if (dist < checkDist)//범위내에 player
{
height = math.abs((player.position.y) - (transform.position.y));
if (height < 6)
{
target = player;
say("!");
}
else
{
target = waypoint[wayNumber];
say("?");
}
}
else
{
target = waypoint[wayNumber];
say("");
}
Vector2 onlyXTarget=new Vector2(target.position.x,transform.position.y);
transform.position = Vector2.MoveTowards(transform.position, onlyXTarget, 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);
}
if ((target.position.x == transform.position.x)&&(target.position.x == waypoint[wayNumber].position.x))//y축 추가로 넣어도 되는데 넣을꺼면 바닥에 딱붙여야됨
{
wayNumber++;
wayNumber = wayNumber % 2;
target = waypoint[wayNumber];
}
Jump();
}
private void FixedUpdate()
{
isground = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
iswall = Physics2D.OverlapCircle(wallCheck.position, groundRadius, whatIsGround);
}
void Jump()
{
if (iswall == true) //벽감지시 점프
{
if (isground == true) //점프 중이지 않을 때
{
rigid.AddForce(Vector2.up*jumpPower, ForceMode2D.Impulse); //위쪽으로 힘을 준다.
isground = false;
}
else return; //점프 중일 때는 실행하지 않고 바로 return.
}
}
private void say(string text)
{
textMeshPro.SetText(text);//string받아서 출력
}
}