- Home /
Sound through wall / Audio zones
Hi there, I have an issue implementing some of my sounds.
For exemple, I have a cubic room. Against one of the wall there is a Hifi playing sounds. When I am in the room, I can hear the Hifi and the sound's volume goes down as I move away from it. So far so good. The only thing is that on the other side of the wall (in another room), I can still hear the Hifi as if the wall wasn't there! The sound should be off or at least really softer.
I don't know how to fix this since I can only deal with spheric audio zones.
Thank you in advance for reading me and helping me out!
Worst part is, F$$anonymous$$OD has this functionality out of the box- it's just that Unity doesn't have any interface to it, and so you can't implement the native F$$anonymous$$OD solution. You need to rely on hacks like audio zones.
I know this doesn't help you now, but I'll be releasing a rather cool audio toolkit in the Asset store in a couple months that would take care of this for you.
Im having the exact same problem at the moment, I have tried to do this using RayCast but I cannot get it working proberbly since there is no 360 degree Raycast implemented.
360° raycast? why? all you need to do is cast a raycast (or sphere cast) from the listener to every audiosource that should be "silenced" if not visible. If it's not visible, you can change the roll-off values so it's fading-out earlier.
@Bunny83 How would you go about doing that? I can't seem to find a solution for casting rays to multiple objects
Answer by Bunny83 · Mar 02, 2012 at 12:46 PM
This is a theoretical solution. I haven't tested it yet and you have to play around with the distance values.
// C#
public class OccludableAudio : MonoBehaviour
{
private Transform m_MyTrans;
private AudioSource m_Source;
private float m_MaxDistance;
public Transform Listener;
public float OccludedDistance = 5.0f;
public float FadeSpeed = 10.0f;
public LayerMask Mask;
void Start()
{
m_MyTrans = transform;
m_Source = audio;
m_MaxDistance = m_Source.maxDistance;
}
void Update()
{
float target;
if (Physics.Linecast(Listener.position, m_MyTrans.position, Mask.value))
target = OccludedDistance;
else
target = m_MaxDistance;
m_Source.maxDistance = Mathf.MoveTowards(m_Source.maxDistance, m_Target, Time.deltaTime * FadeSpeed);
}
}
You have to attach this script to every audio source that should be able to be occluded. You have to assign your player to the Listener transform variable. Another way would be to make the Listener variable static and assign it from the player's script.
public static Transform Listener;
// In your players script:
void Start()
{
//[...]
OccludableAudio.Listener = transform;
}
This way you only have to assign it once.
Don't forget to setup the layermask to specify what colliders should occlude the audio.
Answer by DaveA · Mar 02, 2012 at 01:19 AM
You could set up some, say, Box colliders and use them as proximity detectors to tell the audio to attenuate accordingly. You'd want to change the volume the player hears rather than the source if it's multi-player (or at least, not propagate the volume to other players).
Your answer
Follow this Question
Related Questions
Vehicle help 1 Answer
3D Audio Source plays choppy/poping in web player (Video) 1 Answer
I need help about audio.PlayOneShot!! 1 Answer
how to fix 2d sound distortion? 1 Answer