How to make enemy move through walls?

I am working on an enemy that should only move when the player is not looking at it. I tried using the Renderer.isVisible and everything works but if I look in the direction of the enemy through a wall he also stops moving. The isVisible method counts it in camera view even if a wall is between the player and the enemy. I also tried OnBecameVisible()/OnBecameInvisible() but I get the same result. Does anyone know how to solve it?
Here is the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AI : MonoBehaviour
{
    public GameObject deathScreen;
    public UnityEngine.AI.NavMeshAgent agent;
    public Animator endo;
    public Transform player;

    public LayerMask whatIsGround, whatIsPlayer;
    
    //Patroling
    public Vector3 walkPoint;
    bool walkPointSet;
    public float walkPointRange;

    //Attacking
    public float timeBetweenAttacks;
    bool alreadyAttacked;
    bool isDead;

    //States
    public float sightRange, attackRange;
    public bool playerInSightRange, playerInAttackRange;

    private void Awake()
    {
        player = GameObject.Find("Player").transform;
        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
    }

    private void Update()
    {
        if(agent.GetComponent<Renderer>().isVisible)
        {
            endo.speed = 0f;
            agent.speed = 0f;
        }
        else
        {
            endo.speed = 1f;
            agent.speed = 10f;
        }
        //Check for sight and attack range
        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);

        //if (!playerInSightRange && !playerInAttackRange) Patroling();
        if (playerInSightRange && !playerInAttackRange) ChasePlayer();
        if (playerInAttackRange && playerInSightRange) AttackPlayer();
    }

    private void Patroling()
    {
        if (!walkPointSet) SearchWalkPoint();

        if (walkPointSet)
            agent.SetDestination(walkPoint);

        Vector3 distanceToWalkPoint = transform.position - walkPoint;

        //Walkpoint reached
        if (distanceToWalkPoint.magnitude < 1f)
            walkPointSet = false;
    }
    private void SearchWalkPoint()
    {
        //Calculate random point in range
        float randomZ = Random.Range(-walkPointRange, walkPointRange);
        float randomX = Random.Range(-walkPointRange, walkPointRange);

        walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);

        if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
            walkPointSet = true;
    }

    private void ChasePlayer()
    {
        endo.SetBool("Walk", true);
        agent.SetDestination(player.position);
    }

    private void AttackPlayer()
    {
        //Make sure enemy doesn't move
        agent.SetDestination(transform.position);

        transform.LookAt(player);

        if (!alreadyAttacked)
        {
            if(isDead == true)
            {
                FindObjectOfType<Movement_Player>().sensitivity = 0f;
                Time.timeScale = 0f;
                Cursor.lockState = CursorLockMode.None;
                return;
            }
            ///Attack code here
            Instantiate(deathScreen, new Vector3(0f,0f,0f), Quaternion.identity);
            isDead = true;
            //FindObjectOfType<AudioController>().Play("AIShoot");
           
            ///End of attack code

            alreadyAttacked = true;
            Invoke(nameof(ResetAttack), timeBetweenAttacks);
        }
    }
    private void ResetAttack()
    {
        alreadyAttacked = false;
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, attackRange);
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, sightRange);
    }
}

@Sayuuddx - The best way to solve this is to use a raycast method. It will only return true if there are no obstacles between the player’s view and the enemy. This is more precise because it only considers the enemy visible if there is direct line-of-sight from the player to the enemy. Let’s adapt your code to use a raycast.

public class AI : MonoBehaviour
{
    //Your other variables here...

    private void Update()
    {
        //Your other code here...
        
        if(IsPlayerLookingAtEnemy())
        {
            endo.speed = 0f;
            agent.speed = 0f;
        }
        else
        {
            endo.speed = 1f;
            agent.speed = 10f;
        }
        
        //Your other code here...
    }
    
    private bool IsPlayerLookingAtEnemy()
    {
        Vector3 directionToEnemy = transform.position - player.position;
        RaycastHit hit;
        
        // Check if the raycast hit something before hitting the enemy
        if (Physics.Raycast(player.position, directionToEnemy, out hit))
        {
            // If the first thing it hits is the enemy, then the player is looking at the enemy
            if (hit.transform == transform)
            {
                return true;
            }
        }
        return false;
    }
    
    //Your other methods here...
}

What we did was to replace the isVisible check in the Update method with a new method called IsPlayerLookingAtEnemy(). This new method does a raycast from the player to the enemy. If the raycast hits something before hitting the enemy, the method returns false (indicating that the player is not looking at the enemy). If the first thing the raycast hits is the enemy, the method returns true. This will solve the issue of the enemy being considered visible even when there is a wall between the player and the enemy.

It doesn’t work like it should. I want the enemy to not move when it is in camera view, the solution you gave me only works when the player is looking straight at the enemy which is not what I wanted.

I have an idea, create an “Enemy” tag for the enemy, assign the tag to him, then

public string enemyTag;
public float range = 100;
private bool PlayerLookingEnemy(){
      if (!Physics.Raycast(player.position, enemy.position-player.position, range, out RaycastHit hit))
          return false;
      if (hit.transform.compareTag(enemyTag))
          return enemyRenderer.isVisible;
      else
          return false;
}

It is really this easy!