• Register

HoD is a first person point-and-click adventure set in a steampunk environment. You will have to unravel the mystery of the airship, you will find yourself in, where nothing is what it seems.

Report RSS Sound behind the wall

My approch of fading sound effects in Unity in an interior scenario.

Posted by on - Basic Sound Effects

When using audio sources in unity, they come standard with a fall-off setting to control the volume fall-off over distance. This may work fine in an open level but will not take obstacles into account, like walls between the source and the player.

reverb zone

Ok, so why not use reverb zones for this? They can mute or alter the sound while the player is inside.

However, this is only useful for sounds that are directly attached to the player. The most common use-cases are footsteps, gun shots and so on. And they only come in one handy shape…, spheres.

So let's build our own reverb zone that affects the source and not the listener and can have any shape. This will only work for single-player games though, as it will change the source, not each player’s sound perception.

rooms

When the player walks into the next room, the sound emitted inside the distant room will fade gradually, with the door still open. The volume level will decrease even more while the door is closing. But it will not be turned off completely.

Depending on the intensity of the sound, it will still be audible behind the wall but a lot duller and muffled.

reverb filter

To achieve the muffled sound, every audio source is equipped with a reverb filter. It will work just like the reverb zone but it only affects the source it is attached to.

I set up the reverb filters to work for the room where the audio source resides, to have the correct reverb effect while the player is inside the room. As soon as he leaves, the controller will adjust some of its properties to make it sound dull, like dry level, reverb level and room HF.

Make sure to use the user preset. Otherwise you will not be able to change the settings during runtime in the code. You can also switch presets in code before modifying the values.

trigger

To figure out if the player is inside or outside the room, a simple box collider will do. When the player walks about outside the trigger, the controller will determine the current door states and the distance to the player.

Due to the fact that most walls are perfectly straight and not spherical, I added a separate distance fall-off function for the volume level by checking the distance for one axis only, as most rooms are only accessible from one side.

Some code snippets:

// the audio source components
var audioSource : AudioSource[];
// the reverb filter components
var reverbFilter : AudioReverbFilter[];

// before modifying the source, store its current settings
fSetVolume = new float[audioSource.length];
for (var sound : AudioSource in audioSource) {
	fSetVolume[i]= sound.volume;
	i++;
}
// do the same for all filter settings you need to modify
fReverbSet = new float[reverbFilter.length];
...

// changes the source volume depending on the distance to the player and the door state
for (var sound : AudioSource in audioSource) {
	sound.volume = fSetVolume[i]* FallOff() * DoorFader();
	i++;
}

// define the filter settings for the sound you want behind the wall
public var dryLevel : float;
public var reverbLevel : float;
public var roomHF : float;

// now change the reverb filter settings
var fFader //distance to the gap in the wall, if there is no door
if(playerInZone) { // inside the room
	for (var filter : AudioReverbFilter in reverbFilter) {
		filter.dryLevel = fReverbSet[0];
		filter.reverbLevel = fReverbSet[1];
		filter.roomHF = fReverbSet[2];
	}
} else if (doorfFaderEnabled) { // outside and door available
	for (var filter : AudioReverbFilter in reverbFilter) {
		filter.dryLevel = Mathf.Lerp(dryLevel,fReverbSet[0],fDoorProgress);
		filter.reverbLevel = Mathf.Lerp(reverbLevel,fReverbSet[1],fDoorProgress);
		filter.roomHF = Mathf.Lerp(roomHF,fReverbSet[2],fDoorProgress);
	}
} else { // outside with no door fader
	for (var filter : AudioReverbFilter in reverbFilter) {
		filter.dryLevel = Mathf.Lerp(fReverbSet[0],dryLevel,fFader);
		filter.reverbLevel = Mathf.Lerp(fReverbSet[1],reverbLevel,fFader);
		filter.roomHF = Mathf.Lerp(fReverbSet[2],roomHF,fFader);
	}
}

What does it sound like?
Go up to the engine romm and listen to the sound while the door is opening and closing.

Indiedb.com

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: