using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AIChace : MonoBehaviour
{

    public GameObject Player;
    public string triggeringItemTag = "YourItemTag";
    public int range = 7;

    public float speed;

    private float distance;
    private Rigidbody2D body;
    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        distance = Vector2.Distance(transform.position, Player.transform.position);
        Vector2 direction = Player.transform.position - transform.position;
        direction.Normalize();
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        if (distance < range)
        {
            body.velocity = direction * speed;
            transform.rotation = Quaternion.Euler(Vector3.forward * angle);
        }
        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Bullet"))
        {
            Destroy(gameObject);

        }
    }
}