- Home /
How to have audio only be heard in a certain radius?
Hi, I'm a sophomore in high school learning Unity and I'm kind of a noob. I've made a few games here and there for myself but one thing I don't know how to work to well with is audio. I have an audio source on a game object that is a campfire. The audio that it is playing is the sound of a crackling fire, but when I play the game, I can hear it from far away. That is the only sound in the game as of right now and it plays as if it's background music, which I don't want. I want it to be heard only in a certain radius, say two unity units. I want it to be like real life, where you can hear a sound getting stronger as you get near it. No not neccessarily like a doppler effect, but more like the sound only plays in a sphere or something. I'll be as clear as possible as log as you can help me. Thank you in advance. No, I do not have any scripts attached to the campfire, but I will script if it's needed to acheive what I want. Thank you
Answer by zzeeshann · Feb 24, 2017 at 01:22 AM
You need to do three things on Audio Source
Spatial Blend from 2D to 3D.
Volume Rolloff from logarithmic to linear.
Set Minimum and Maximum distance.
This video will show how to do this: https://youtu.be/LrchScd806w
This worked great. It's up to date with Unity 5.5 and it does exactly what I want it to do. Thank you @zzeshann. you couldn't have done a better job, thanks
Answer by fred_gds · Feb 22, 2017 at 07:01 PM
Well did you take a look at the documentation?
https://docs.unity3d.com/ScriptReference/AudioSource.html
Just set the Audio to 3D and use AudioSource.maxDistance for the effect of the sound getting louder.
Answer by paulbuck86 · Mar 16, 2019 at 05:30 PM
For anyone still having issues, I found that if you do not have an "audio listener" on your player. You will hear all sounds. Make sure you have an audio listener on your player.
Answer by no00ob · Jan 10, 2018 at 07:19 PM
thanks solved my issues, my problem was it was 0.995 so not at 3D and this messed with it xD