How do I set a distance for audioSource ???

Hello

How do I set a distance for audioSource ???

thx >>>

by set a distance do you mean that the sound should play only when sound source is within some distance?

–

4 Answers

4

You don’t need any script to do that: in the AudioSource, select Rolloff Mode = Linear Rolloff, Max Distance = 5, Min Distance = 4.5 (or other value a little lower than the Max Distance) - this will make the sound disappear when the distance to the audio source is 5 or greater.

how this distance gets calculated? Based on camera position.

–

For thoose who can’t get a clue why distance to listener looks calculated so strange in 2D - check you Z for camera and for AudioSource!

Thank you a lot!

–

Thank you! And one more important thing is to toggle "Spatial blend" to 3D

–


But for some reason, i hear the sound everywhere, at the same rate. I think i made something wrong, i’m newbie to Unity.

Please do not revive or hijack old questions with your own question. What you posted is not an answer. Create you own separate question. You may want to look into the spatial blend setting. Currently your audio source is a fully 2d source so no 3d calculations will be applied. https://docs.unity3d.com/ScriptReference/AudioSource-spatialBlend.html

–

If you are using Unity 5, find 3D Sound Settings at the bottom of the Audio Source component, expand it, and then find Max Distance and set it to whatever value you want.