Enemy line of sight

I’m having trouble with raycasting! Basically, my enemy has a sphere collider I’m using as its maximum sight distance, and when the player enters this, it tells the enemy to chase the player. That part works fine, but the problem is that the enemy can “see” the player through walls because the collider goes through walls. I’m not fussed about field of view since these enemies are supposed to have a 360 degree line of sight.
I’m trying to use raycasts so the enemy will ignore the player if there is a wall in between them, but nothing I’m trying has worked so far. Here’s my enemy sight code, shamelessly looted from the stealth tutorial.

{
	private GameObject playerTarget;
	private SphereCollider col;
	
	void Awake ()
	{
		playerTarget = GameObject.FindGameObjectWithTag ("Player");
		col = GetComponent<SphereCollider>();
	}

	void OnTriggerEnter (Collider other)
	{
		if(other.gameObject == playerTarget)
		{
			Vector3 direction = playerTarget.transform.position - transform.position;
			{
				RaycastHit hit;
				
				if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))
				{
					if(hit.collider.gameObject == playerTarget)
					{
						Debug.Log ("player in sight");
						//Stuff happens that makes the enemy chase the player
					}
				}
			}
		}
	}
}

This code doesn’t work 100% of the time. Sometimes I get the debug “player in sight” sometimes I don’t, if the player goes into the collider when a wall is between them, nothing happens, but re-entering the collider with nothing in the way still has nothing happen. It’s like the raycast happens only once, regardless of how many times the player enters and exits the collider sphere.
Trouble is the tutorial never really explains exactly what a raycast is and how they work. Are they linear beams or do they fill the entire sphere?

Your problem is that the raycast is only used when the player enters the trigger, then you would need the player to get out and back in to call a new raycast.

You should either set a boolean and use it in the update or check for distance instead of using collision.

See this as well,that may help you unitygems.com - unitygems Resources and Information.