How do I make the enemy script/AI stop following me when It is in view of the player camera?

Hi! I’m pretty new to unity and C#. I’ve currently been making my game for about 2 weeks now and I’m stuck with the enemy script where it needs to stop when I look at it. Here is what I’ve done so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoveEnemy : MonoBehaviour
{
     public Transform Player;
     int MoveSpeed = 6;
     int MaxDist = 10;
     int MinDist = 5;

    void Start()
    {

    }

    void Update()
    {
        transform.LookAt(Player);
        if (Vector3.Distance(transform.position, Player.position) >= MinDist)
        {
            transform.position += transform.forward * MoveSpeed * Time.deltaTime;
            if (GetComponent<Renderer>().isVisible)
            {
                MoveSpeed = 0;
            }
            if (!GetComponent<Renderer>().isVisible)
            {
                MoveSpeed = 6;
            }
        }
    }
}

Even though I am no longer looking at it it still moves and I’m stuck here. if (!GetComponent().isVisible) Does not seem to work. I’ve also read multiple old posts about this that don’t work anymore. Any tips?

See this post about the same problem. The solution by the user duck is more technical, but the answer by runevision works in a pinch. One of the provided answers also has a code snippet that you can copy (one of the solutions suggested by duck).