using System;
using UnityEngine;
using UnityEngine.Serialization;

public class FindPlayer : MonoBehaviour
{
    [SerializeField]
    private Transform player;
    [SerializeField]
    private Transform self;
    [SerializeField]
    private LayerMask layerMask;

    [HideInInspector]
    public bool playerFound=false;


    private bool seen=false;
    private void OnTriggerEnter(Collider other)
    {
        if (other.transform.tag == "Player")
        {
            seen = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.transform.tag == "Player")
        {
            seen = false;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Vector3 direction = (player.position - self.position).normalized;
        Gizmos.DrawLine(self.position, transform.position + direction * 10f); // Adjust length as needed
    }

    private void LateUpdate()
    {
        if (seen)
        {
            RaycastHit hit;
            Physics.Raycast(self.position, (player.transform.position - self.position).normalized, out hit, layerMask);
            playerFound = false;
            if (hit.collider != null && hit.collider.name == "Player")
            {
                playerFound = true;
            }
        }
    }
}
