using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Forever_Chase : MonoBehaviour
{
// (유령이) 플레이어를 계속 추적한다(Chase)
public float speed = 3; // 유령의 이동속도
public string targetObjectName; // 목표 오브젝트의 이름 저장
// 리지드바디컴포넌트형 변수선언(클래스명 변수명 = new 클래스명())
Rigidbody2D rb;
// 게임오브젝트형 변수선언
GameObject targetObject;
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.gravityScale = 0;
rb.constraints = RigidbodyConstraints2D.FreezeRotation;
// 목표 오브젝트를 찾는다.
targetObject = GameObject.Find(targetObjectName);
}
void FixedUpdate() //일정한 속도로 진행
{
// 목표 오브젝트의 방향을 조사한다. 방향을 조사해서
Vector2 dir = (targetObject.transform.position - this.transform.position).normalized; // 벡터형 변수
// 그 방향으로 (계속) 이동한다.
float vx = dir.x * speed;
float vy = dir.y * speed;
rb.velocity = new Vector2(vx, vy);
// 유령이 이동방향을 바꿀때 반전하기
this.GetComponent<SpriteRenderer>().flipX = (vx < 0);
}
}