How do I use RayCasting to play a sound when my player is close to an enemy

I want to have a sound play when my player is close to an enemy AND only when that enemy is visible ( i’ll be using renderer.isVisible ) If my enemy comes up behind me and i turn around a sound doesn’t play . I tried used trigger zones but i couldn’t get that to work, and i’m not very familiar with raycasting so help is appreciated. (could it be possible to have a trigger on the player so when the enemy entered it and was visible it played? how could I refer the enemy to the render.isVisible)

my current script `
function OnTriggerEnter(other : Collider){

if(other.gameObject.tag == “Player” && renderer.isVisible){
audio.Play();
}
}`

FOR ANYONE INTERESTED FINAL SCRIPT IS

function OnTriggerStay(other : Collider){

    if(other.gameObject.tag == "Player" && renderer.isVisible && !audio.isPlaying){
   
   audio.Play(); }}

Ah, I haven’t tested this but have you tried changing “OnTriggerEnter” to “OnTriggerStay”? I think your issue is that your ‘Player’ enters the collider while the renderer isn’t visible, and when the renderer IS visible you aren’t entering the collider, you’re already in it.

   function OnTriggerStay(other : Collider){
         
        if(other.gameObject.tag == "Player" && renderer.isVisible){
        audio.Play();
        }
        }