- Home /
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 :)
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 :-)
I am new to Unity, so this may be a bad idea, but what about attaching this sound to the player ins$$anonymous$$d of the ocean, and just play it based on proximity to the water? Seems like that might be easier than having to have AudioSources spread around?
Answer by DaveA · Oct 07, 2013 at 12:27 PM
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).
Answer by jakejolli · Mar 07, 2016 at 07:08 PM
**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.
Answer by Llama_w_2Ls · Jan 26, 2021 at 01:02 PM
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.
Your answer
Follow this Question
Related Questions
Destroying object without interrupting audio source? 2 Answers
How to play music/audio 2 Answers
How do I play audio once in Update? 1 Answer
Muiltiple Audio Sources Pitch 2 Answers