Ambient Audio

Because the user should inhabit their virtual selves as much as possible, and their virtual self is also lying down, I thought to emulate the effect of muffling the sound in one ear when it is pressed down against the ground. Of course, the actual user is wearing earbuds or headphones, so the sound will not be muted, but since I can track the angle their head is positioned in, I can fake it by panning the ambience away from the ear that is pressed down.

Ideally, the master sound channel would be muted on the muffled side, but panning an audio mixer channel does not appear to be possible. Unless there’s something I’m missing, this is the first time I’ve run up against a limitation of Unity, rather than my own ability or budget.

Anyhow, individual 2D audio sources can be panned. So, I can at least control the ambient sound. First, to figure out how to determine if the user’s head is tiled to one side. I can read their output and determine the range of values that represent the user tilting their all the way to the right or left. Once I had those , I used Mathf.InverseLerp to turn the range of angles into a float from 0 to 1. The following is called form the Update() function:

void MuffleAmbience (){
    float panningAmount = 0.0f;

    if (Camera.main.transform.eulerAngles.z > 60 && Camera.main.transform.eulerAngles.z < 86 ) {
      //	The user's head is tilted to the left.
      panningAmount = Mathf.InverseLerp( 60, 86, Camera.main.transform.eulerAngles.z );
    } else if (Camera.main.transform.eulerAngles.z > 274 && Camera.main.transform.eulerAngles.z < 300) {
      //	The user's head is tilted to the right.
      panningAmount = Mathf.InverseLerp( 300, 274, Camera.main.transform.eulerAngles.z ) * -1;

    ambience.panStereo = panningAmount;

Mathf.Lerp returns a value from 0 to 1.0 represented how far far between two numbers a third number is. For example, 8 is halfway between 6 and 10, so Mathf.InverseLerp (6,10,8) would return 0.5. I do two checks, one each for left and right, and then set the ambience’s stereo panning respectively. This only occurs at the edge of the range of movement, so the sounds are not muffled in either channel until the user’s head is just about resting on the pillow. I also cut off the range a bit so it doesn’t go fully silent in either channel.

Trying it out, it feels quite natural, and is the sort of feature that isn’t really noticed by the user unless it’s pointed out.The 3D audio of the fireworks playing their whistles, booms and cracks still plays in 3D space regardless of the user’s head orientation though. Perhaps I can come up with another hack for that.