Simulating underwater ambience with audio filters

It doesn't matter how great your graphics are: an underwater scene is never realistic enough without the muffled sound caused by submersion. In this recipe, we will learn how to simulate that effect whenever our player is under water.

Getting ready

For this recipe, we have prepared a basic scene including a camera controlled by the up and down arrow keys. The scene is available inside the AudioFilterScene package, within the 0423_11_04 folder, where you can also find the OceanSound.wav audio file.

How to do it...

To simulate underwater ambience, perform the following steps:

  1. Import the AudioFilterScene package and the OceanSound.wav audio file into your Unity project.
  2. Open the FilterScene level, inside the 11_04 AudioFilter folder in the Project view.
  3. From the Project view, drag the OceanSound asset into the Daylight Simple Water game object in the Hierarchy view.
  4. In the Hierarchy view, select Daylight Simple Water and select the Loop option of the Audio Source component:
    How to do it...
  5. In the Hierarchy view, select the Main Camera. Then, add an Audio Low Pass Filter component to it (Component | Audio | Audio Low Pass Filter).
  6. Change the component's Cutoff Frequency value to 1000:
    How to do it...
  7. Create a new C# script and rename it as UnderwaterSound.
  8. Open the UnderwaterSound script in your editor and replace everything with the following code:
    using UnityEngine;
    using System.Collections;
    
    public class UnderwaterSound : MonoBehaviour
    {
        public GameObject oceanObject;
        public float falloff = 1.0f;
        private AudioLowPassFilter filter;
        void Start(){
          filter =  GetComponent<AudioLowPassFilter>();
          filter.enabled = false;
        }
        void Update()
        {
            if (transform.position.y < oceanObject.transform.position.y){
                filter.enabled = true;
                filter.cutoffFrequency = 1000 - (transform.position.y * falloff);
            }else{
                filter.enabled = false;
            }
        }
    }
  9. Save your script and attach it to the Main Camera by dragging it from the Project view into the game object in the Hierarchy window.
  10. Drag the Water3Example game object from the Project view into the Ocean Object slot of the Underwater Sound component. Also, change the Falloff parameter to 20:
    How to do it...
  11. Play the scene and press the down arrow key to move the camera down. The filter should be activated as the camera goes below the water level, dampening the sound.

How it works...

Once Audio Low Pass Filter has been attached, all we need to do is detect whether the camera is below the water level to enable it, also amplifying its effect by raising the Cutoff Frequency of the filter as the camera goes down even further.

There's more...

The following are some information on how to fine-tune and customize this recipe:

Keeping the Cutoff Frequency unchanged

To keep the Cutoff Frequency the same regardless of how deep in the water the camera is, just change the Falloff value to 0.

Adding more filters

Audio filters can be expensive in terms of processing, but that doesn't mean you can't experiment or use them in you projects. In fact, you could add a second audio filter to this recipe, such as Echo, to make the sound effect more interesting.

See also

  • The Simulating underwater ambience with Audio filters recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.165.62