Raycast Drawing: Best method?

Hello, I am in the early stages of developing a 2D game, using the URP, and I have created a system where the enemy uses multiple ray casts and angles to create a FOV where the player can be detected. I would like to be able to draw these, but I have no documentation of how to do this online, so I am looking for help. I will provide the code:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class Enemy : MonoBehaviour
{
    public PlayerController playerScript;
    public float speed = 2f;
    public float pauseTime = 2f;
    public Transform groundDetection;
    public LayerMask whatIsGround;
    public LayerMask playerLayer;
    public GameObject projectile;
    public float projectileSpeed = 5f;
    public float detectionAngle = 45f;
    public int rayCount = 5;
    public float raycastDistance = 2f;
    public float shootCooldown = 2f;
    public Vector2 spawnPos;
    public Slider healthBar;
    public float enemyDamage;
    public float MaxHealth = 10;
    public float Health = 10;
    private bool movingRight = true;
    private bool isPaused = false;
    private GameObject player;
    private float lastShootTime;
    private Rigidbody2D rb;

    void Start()
    {

        spawnPos = gameObject.transform.position;
        healthBar.maxValue = MaxHealth;
        healthBar.minValue = 0;
        Health = MaxHealth;
        player = GameObject.FindGameObjectWithTag("Player");
        playerScript = player.GetComponent<PlayerController>();
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (Health <= 0)
        {
            gameObject.transform.name = "Enemy(Killed)";
            gameObject.transform.position = new Vector2(300, 0);
        }
        else
        {
            gameObject.transform.name = "Enemy(Alive)" + "(" + Health + ")";
        }
        if (Health >= MaxHealth)
        {
            Health = MaxHealth;
        }
        healthBar.value = Health;
        if (!isPaused && !PlayerDetected())
        {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }

        RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, 2f, whatIsGround);
        if (groundInfo.collider == false)
        {
            if (!isPaused)
            {
                StartCoroutine(PauseAndTurn());
            }
        }

    }

    IEnumerator PauseAndTurn()
    {
        isPaused = true;
        yield return new WaitForSeconds(pauseTime);
        if (movingRight)
        {
            transform.eulerAngles = new Vector3(0, -180, 0);
            healthBar.transform.localScale = new Vector3(-1, 1, 1);
            movingRight = false;
        }
        else
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            healthBar.transform.localScale = new Vector3(1, 1, 1);
            movingRight = true;
        }
        isPaused = false;
    }

    bool PlayerDetected()
    {
        float rayAngleStep = detectionAngle / (rayCount - 1);
        Vector2 direction = player.transform.position - transform.position;
        for (int i = 0; i < rayCount; i++)
        {
            float rayAngle = -detectionAngle / 2 + rayAngleStep * i;
            Vector2 rayDirection = RotateVector(direction, rayAngle);
            RaycastHit2D playerInfo = Physics2D.Raycast(transform.position, rayDirection, raycastDistance, playerLayer);
            if (playerInfo.collider == true)
            {
                if (Time.time > lastShootTime + shootCooldown)
                {
                    ShootPlayer();
                    lastShootTime = Time.time;
                }
                return true;
            }
        }
        return false;
    }

    void ShootPlayer()
    {
        GameObject proj = Instantiate(projectile, transform.position, Quaternion.identity);
        Rigidbody2D rb = proj.GetComponent<Rigidbody2D>();
        Vector2 direction = (player.transform.position - transform.position).normalized;
        rb.velocity = direction * projectileSpeed;
    }

    Vector2 RotateVector(Vector2 v, float degrees)
    {
        float radians = degrees * Mathf.Deg2Rad;
        float sin = Mathf.Sin(radians);
        float cos = Mathf.Cos(radians);
        float tx = v.x;
        float ty = v.y;
        return new Vector2(cos * tx - sin * ty, sin * tx + cos * ty);
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.CompareTag("Bullet"))
        {
            Health -= playerScript.damage;
        }
    }
}

If you want the lines just for helping you to visualize the rays while testing then you can use Debug.DrawRay or Debug.DrawLine. But if you want the players to be able to see the rays when playing your game then you can use the LineRenderer.

By the way, you can give your enemy a FOV by using Vector2.Dot to quickly test if the enemy is looking in the general direction of the player, and only then do you need to do a single ray cast from the enemy to the player to test if there’s nothing blocking the enemy’s view of the player.

There are some occasions where a few ray casts can be better. For example the player may be peeking around a corner and so a single ray cast wouldn’t reach the player’s origin and yet the enemy should still be able to see the exposed side of the player. So in this situation you have to do three very specific ray casts - one to the player’s origin, and one to the left and right sides of the player.

Hello, thank for the reply, I am wondering on how to get this to actually work, I have tried but am still a little bit confused can you provide documentation or a code snippet? Thank you in advance!

using UnityEngine;
public class EnemyAI : MonoBehaviour
{
    public Transform player;

    void Update()
    {
        Vector2 playerDirection=(player.position-transform.position);
        if (Vector2.Dot(playerDirection.normalized,transform.right)>0.5f)    // player within our field of view?
            if (Physics2D.Linecast(transform.position,player.position).transform==player) // Do we have line of sight with the player? They could be hiding behind a wall..
                transform.right=Vector3.Lerp(transform.right,playerDirection,5*Time.deltaTime); // gradually look towards the player
    }
}

Hello, my team has found an alternative for the enemy, and instead this code has been repurposed and reworked to be on the player as a ¨Fog of War¨ gimmick, the player is using a separate game object with the following components:
-Mesh Renderer
-Mesh Filter
-The Script(Field Of View)

I just need to work on the idea of creating a mask, so the player can see the level, but things like enemies are hidden, until the player can actually see the enemy, we want the player to have to make LOS of a enemy in order to see them, I have tried using sprite renderers and enabling and disabling them, but to no avail. Is there some other way to create a mask effect using a mesh renderer/filter to create a mask in the 2D URP? does somebody have some suggestions? I am really lost, Everything found is unable to work with the URP, and lighting is an important part of the project. Thanks for help in advanced!