AI shooting through walls

I have an enemy AI script I got online and it works great except that the bad guy will shoot at me through walls. I searched and found a lot of the same question but no answers. I think what is the problem is this line of code that looks like it just seeks me out by distance. I think it should be a raycast of some kind?

I’ll keep plugging away at it but any help would be greatly appreciated . Here is probably the detection code I need to alter. If you need more info I can paste the whole script but I think this is the culprit

		if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
			PlayerDetected = true;
			if (PlayerDetected)

Your current code only takes into account the distance to the player to decide whether the player is detected or not, that is, it does not consider any obstacle or wall that might hide the player.

You would need to use raycasting to able the player to hide behind walls. Have a look at the documentation. It contains several examples about using raycasting too: