- Home /
Player position within audio source range
I'm trying to come up with a way to determine if the player is within the range of an audio source, and I'm having trouble figuring out way to do it. I've looked through various answers, forum posts, and scripting reference materials. I've experimented with using triggers or collisions within the source, and just make a sphere collider the same size of the range. It's not any particular audio source though, but all of them. I need to find a way to check if the player is within any particular audio source in general, and if they are within range then the can do x, else y happens.
Answer by Benproductions1 · Jul 15, 2013 at 03:35 AM
Hello,
AudioSources do not have a range. The volume of the sound that comes out of Unity is determined by a fall off. This fall off is determined per AudioSource in the "3d Sound Settings".
The default falloff settings are logarithmic and determined by a pre-defined curve. As of now, there is no direct way of accessing that curve to find the volume based on distance.
However there are two things you can do: 1. Use the "MaxDistance" and apply your own falloff to mathematically determine the volume at any given point. 2. Determine the "volume" at the point of the AudioListener by analysing the output sound data.
Neither of the ways are nice (or easy if you don't know how), but they do work :)
Hope this helps,
Benproductions1
I wouldn't really need to know volume, just to know if player was within $$anonymous$$axDistance. But, if thats the case, it seems like an on trigger collider would be the best bet, or easiest. So essentially, I would add a collider to every sound source, and add an OnTriggerStay script to the player to check if when he enters a trigger collider that is a component of an audiosource then do stuff. Would that be the way to do it then?
The question is: At what point does the volume fall off to 0. I highly suggest against using triggers. They generate an unessecarily large overhead. Just "Find" all audio sources and calculate the distance. If that distance is smaller than something, your character can hear it.
How is the overhead on that compared to triggers? That was one of the things I was afraid of in doing that, because I would have to do a search for ALL audio sources on a map. For the concept I'm going for, the $$anonymous$$Distance would be 10, for example, and the maxDistance would be 10.1 (since I can't have them both 10.
I guess the way I said "add a collider to ever sound source" was a bit misleading now that I look at it. In my case, I may many audio sources in a scene, but only maybe 6 or so would be ones that need the player to respond to. I can tag them 'special' or something, and do a search for audio sources with tags of 'special' and see if I'm within x distance to them, or add a trigger to each of the 6 and check player collides with it.
So comparing those two methods, which is more efficient? It seemed unnecessary to search for audio during every frame as the player moves through a scene.
There is no overhead. Just "Find" all your audio sources when the scene loads, "cache" them and then check the distance. $$anonymous$$y method checks for distance, while yours checks for collision. $$anonymous$$ine will always be faster (as long as you cache your audio sources)
Your method also means, that if you want to use triggers for anything else, you will have to add exceptions for colliding with certain people.
Your answer