How to make a FoV to detect enemies

Hi there, I’ve been banging my head against a wall for about a month trying to figure out how to make an “Field of view” for the player. I’ve looked at about 5 different videos and following step by step, and it never works. This is the code for the most recent attempt, and it won’t even work with the editor. If someone could help me out it would be a lifesaver!

Fov code:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class FoV : MonoBehaviour
{
    public float Radius;
    [Range(0,360)]
    public float angle;

    public GameObject Safe, safe_2 ,safe_3, owie;

    public LayerMask safe_to_look;
    public LayerMask AAAAAAAAAAAA;
    public LayerMask WallsnJuank;

    public bool likezoiksscoob;

    private void Start()
    {
        Safe = GameObject.FindGameObjectWithTag("Safe");
    }

    private IEnumerator FOVRoutine()
    {
        
        WaitForSeconds wait = new WaitForSeconds(0.2f);
        while (true)
        {
            yield return wait;
            FOVcheck();
            
        }
    }
    private void FOVcheck()
    {
        Collider[] rangecheck = Physics.OverlapSphere(transform.position, Radius, safe_to_look);
        if (rangecheck.Length != 0)
        {
            Transform safe = rangecheck[0].transform;
            Vector3 directionToTarget = (safe.position - transform.position).normalized;

            if (Vector3.Angle(transform.position, directionToTarget) < angle / 2)
            {
                float distancetotarget = Vector3.Distance(transform.position, safe.position);

                if (!Physics.Raycast(transform.position, directionToTarget, distancetotarget, WallsnJuank))

                    likezoiksscoob = true;
                else
                    likezoiksscoob = false;
            }
            else

                likezoiksscoob = false;
        }
        else if (likezoiksscoob)
        {
            likezoiksscoob = false;
        }

    }
}

I see a little issue in your FOVcheck function. The problem is with the Vector3.Angle. Instead of using transform.position, you should be using transform.forward to get the forward direction of your player. Try changing this line:

if (Vector3.Angle(transform.position, directionToTarget) < angle / 2)

to

if (Vector3.Angle(transform.forward, directionToTarget) < angle / 2)

That should fix the problem. Give it a shot and let me know if it works!