Static Sound

I’m trying to make an educational slender project to learn c#. I’m kinda stuck at the static sound effect when looking at him. I want to make what when i look at him the static rises and when i turn away it fades. (I have 2 separate clips for this)
I’ve been trying to figure this out for days now and i have not found answers on google.
Here is my code

 void OnWillRenderObject() {
		

        RaycastHit hit;

        if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit)) {

            if (hit.collider.gameObject == gameObject)
			{
			 sanity -= 1.5f;
				
				if(soundPlay == false)
				{
					audio.clip = SanityUp;
					audio.Play();
					soundPlay = true;
				}
				else if(sanity < 100 && soundPlay == true)
			{
			    soundPlay = false;
				audio.clip = SanityDown;
				audio.Play ();
			
			}

Your title is quite misleading. I thought you meant a static noise as in static object.

Well, so you want that facing the guy, you have one sound, facing away you have a different sound.
Use a line of sight algorithm.

public Transform target;
public AudioSource audioSource; // Drag the bad guy in here.
float _range = 10f;

void Update()
{
    if(IsFacing(target)) audioSource.Play()
    else audioSource.Stop();
}
bool IsFacing(Transform target)
{
    Vector3 __direction = target.position - transform.position;
    if(Vector3.Dot(__direction.normalized, transform.forward)< _range)return true;
    else return false;
}

This goes on the player and target is the bad guy. This is not taking care of the distance but you had that already.
Range is to define the angle. It is in radians so 0.7 is about 45 degrees so that make it 90 degrees (45 on each side).

I would recommend to access the sound on the bad guy so that you can use 3d sound which has doppler effect and mostly the distance attenuation. So where you have High creek creek you would change the sound that the bad guy plays.

Your problem is that when you are facing at the guy, you are toggling the sounds every other frame!

Read your own code. When you first look at the guy, soundPlay is false and you enter the if block where you play SanityUp and set soundPlay = true.

Next frame, when you are still looking at the guy (it’s just a frame after), soundPlay = true from previous frame, so your code enters the else block, where you play SanityDown and set soundPlay = false;

And next frame, the cycle repeats. It only ends when you face away from the guy, where whatever last sound clip was playing finally manages to play their whole length without you interrupting it.

Try this instead. You must play the SanityDown sound if NOT facing the guy and if the SanityUp sound had started playing:

RaycastHit hit;
    
if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit) && (hit.collider.gameObject == gameObject)) 
{
      sanity -= 1.5f;

      if(soundPlay == false)
      {
          audio.clip = SanityUp;
          audio.Play();
          soundPlay = true;
      }
}
else if(sanity < 100 && soundPlay == true)
{
    soundPlay = false;
    audio.clip = SanityDown;
    audio.Play ();
}