How do I play an ambient sound from an ocean surrounding an island?

I am making an island and I want the ocean around it to play an ambient “ocean sound”, but I am not sure how I would do that?

I know the basics of audio sources and such, but if I want the same ambient sound to play when near the beach (with a falloff), how would I make the source to be the entire ocean around the island?
Putting the source on the “water” object, wouldn’t do any good, since the water is “under” the island as well.

I am pretty new to the 3D part of game design, so it might just be me being a bit confused :slight_smile:

In short:
How do I make an ambient sound effect play whenever I am near the edge of the island? Preferably with a falloff so the sounds gets softer the further away from the beach you get.

Thanks in advance :slight_smile:

**NOTE: If you’re going to use this, you’ll have to change “FPSController” in the script to whatever you’ve named your player gameobject.

I encountered this issue too. Didn’t find a whole lot of great answers, but here’s the solution I went with:

Create an empty GameObject with an AudioSource component, and assign its AudioClip to be the sound you want played. Place this GameObject near the shore (preferably in the water, where the player can’t go). Duplicate the GameObject, placing more of them around the shoreline. They don’t have to be super close, but I usually position them so that their MaxDistance gizmos overlap a bit. Then give them all a common tag, I used “WaterSound”.

Then, attach this script to some GameObject in your scene (could be your player, or just an empty object):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class WaterSoundHandler : MonoBehaviour {

    public List<AudioSource> waterSounds;
    private GameObject player;
    public float volumeReductionSpeed = .5f;

	void Start () {
        //Find all of the Water AudioSources 
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("WaterSound")) {
            waterSounds.Add(obj.GetComponent<AudioSource>());
        }

        player = GameObject.Find("FPSController");      
    }
	
	void Update () {
        DetermineClosestWaterSound();       
	}

    void DetermineClosestWaterSound() {
        float distance = float.MaxValue;
        AudioSource closestWaterSound = null;

        //Determine which water sound is the closest
        foreach(AudioSource waterSound in waterSounds) {
           float newDistance = Vector3.Distance(player.transform.position, waterSound.transform.position);
            if (newDistance < distance) {
                distance = newDistance;
                closestWaterSound = waterSound;
            }
        }

        DisableDistantWaterSounds(closestWaterSound);
    }

        //Scale up the closest water sound's volume until it's reached 1.
        //Scale down the volume of any water sound that isn't closest.
    void DisableDistantWaterSounds(AudioSource closestWaterSource) {
        foreach(AudioSource waterSound in waterSounds) {
            if(waterSound != closestWaterSource) {
                waterSound.volume -= volumeReductionSpeed * Time.deltaTime;
            } else {
                waterSound.volume += volumeReductionSpeed * Time.deltaTime;
            }
        }
    }
}

I originally tried just enabling/disabling the audiosources, but the 3D sound became a bit jumpy. Slowly changing the volume helps to blend the two audiosources together better when transitioning.

This can also sometimes be a bit jumpy, but the more audiosources (within reason) that you have, the smoother/more natural the transition.

This is a perfect application for Audio Brush, coming soon to the Asset Store. See http://qlcomp.com/?page_id=61 for updates. Should be very soon! (I used this for the exact purpose to class-up an Oculus Rift demo).

Well surely, the best way to do this would be to have an audio source directly next to the player, or on the camera, that contains the sound of the ocean. Then increase the volume, when you get closer to the ocean. Creating lots of audio sources and scattering them around seems a bit over the top.