using UnityEngine;

public class LineOfSight : MonoBehaviour
{
    public GameObject target;

    private void FixedUpdate()
    {
        IsTargetVisible(target.transform.position);
    }

    // this just checks to see if something
    // is blocking the line of sight
    bool IsTargetVisible(Vector3 targetPosition)
    {
        bool isVisible = false;

        // cast a ray from the target to this object
        RaycastHit hit;
        Vector3 rayDirection =  transform.position - targetPosition;
        if (Physics.Raycast(targetPosition, rayDirection, out hit))
        {
            // check if our position is the same as the object that was hit
            if (hit.transform.position == transform.position)
            {
                Debug.DrawRay(targetPosition, rayDirection, Color.cyan);
                isVisible = true;
            }
        }

        return isVisible;
    }
}
