Boolean Reset Problem

Im having a problem when im looking at enemy it plays a sound and sets soundPlay to true.
But when i look away it wont set it to false. Whats wrong with my code:

if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit) && (hit.collider.gameObject == gameObject)) 
{
      sanity -= 1.5f;
 
      if(soundPlay == false)
      {
		  audio.Stop ();
          audio.clip = SanityUp;
          audio.Play();
          soundPlay = true;
      }
}

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

At the moment, since the ray start from the guy, it will always hit the guy and return true but your second check is always false. So you actually never gets inside the if statement.

See your cast is starting from inside the player towards the bad guy, but the cast hits the player’s collider and stops there.

You need to use a layer mask.

You could also use a Linecast instead. Same idea different principle.