Line of sight audio

I’m extremely new to Unity and I don’t really have too much of an idea of coding and scripting and such, but basically I’m making a game and you explore a labyrinth and there is also a creature roaming around. I have done the basics, but I want it so that a song (a jump scare sound) is played when the creature is a.) In the camera’s view and b.) Close to the character, so that the audio clip plays when people properly see the creature.

Could anyone be of help?

Thank you so much for your time,
Josh

Here are the steps to use:

  1. Have a look at dot product (you can check here unitygems.com - unitygems Resources and Information.)

  2. You have pretty much all you need in the link above as it deals with distance and line of sight

  3. Still you need to make sure that the monster is not behind the wall, so use a linecast that you cast from your player to the monster. Make sure you disable the layers (The link also has a linecast example)

  4. Then if all returns true, you can play your scary music

     if(within range && dot product angle less than angle){
        if(!Linecast(player, monster)){
       play music
    }
    

    }

Well so far I have this:

var appear:AudioClip;

function OnBecameVisible() {

    enabled = true;

    audio.PlayOneShot(appear);

}

function OnBecameInvisible () {

    enabled = false;

    audio.Stop();

}

I attach the script to the enemy and choose my sound for the Appear option and I still cannot hear anything?