- Home /
Is it possible to raise an audio sources pitch as an audio listener gets closer?
Hi, I was wondering how you would attack this problem really. I would ideally like the sound the audio source is playing to increase as an audio listener object gets closer. I imagine a way to do this would be to tag the audio source as one thing and tag the listener as another then to check distance between the two.
I'm not sure what functions to use to do this though and I have had a good look around, there doesn't seem to be too much covering this topic. My problem is also made more difficult as I am not using graphics so no raycasting. I'm still fairly new to the coding but I have a decent level of understanding as I have been learning it for the past few months and have had a great success in programming my audio only game so far. All suggestions would be welcome!
Answer by Muzz5 · Feb 21, 2012 at 02:26 PM
You could just use Audio.Pitch and Transform.Distance. It's pretty simple code, I'll write some for you later, if you need it.
EDIT: Seeing as you're a beginner, and I had some free time, I thought I'd write something up. Haven't tested this, though. Please tell me if there are any issues.
var scaleFactor = 0.5;
var object : Transform;
function Update() {
if(object)
var dist = Vector3.Distance(object.position, transform.position);
audio.pitch = dist * scaleFactor;
}
Answer by zakkar · Feb 21, 2012 at 10:22 PM
It's automatically generate, your audio source should be a 3D Sound after that in the parameter you'll have to adjust the Min Distance and the Max Distance. You're sound will increase and decrease according to the parametre you have adjusted
Answer by jakovd · Feb 21, 2012 at 12:57 PM
You could add a script on a player with public GameObject variable i which you could (through editor) drag'n'drop that AudioSource object. In Update() method get source position through its transform and subtract that value from players.transform to get a vector between them. Getting length of that vector gives you distance between them and you can scale (multiply with some float value) that value and multiply with sources pitch to get the affect you want.
on the other hand, this behavior can be realistically achieved if you just add a sound as a 3D sound and let Unity simulate its damping automatically according to listeners position. i'm not sure if this is what you wanted because your post title said you want to change its "pitch" which is not the same as changing its "volume" (which is what you described in post text).
Answer by andaleeni · Feb 21, 2012 at 10:21 PM
Sorry for the confusion I was trying to convey what I have done so far, so yes I have used 3D sound method to convey a sense of the nearness through volume, my question was basically pitch related. From what I can tell then and from what I have found I need to find some way of subtracting both the audio listener and sound sources vector3s from each other, store it in a float and then call the gameobjects pitch component in audio source and tell it to move down and up according to the distance that the float calculating the difference between both objects vector3s are? Is this what you meant? Also if so is there an example of this you could give or point me to, to work this out code wise. I get the principle of what I have to do Its just putting it into practise with the correct code. Thanks for the fast reply. Also if you need an example of this behaviour the example I am trying to match is how FortressCraft handles being close or far away to a relic, the pitch increases the nearer the player gets to it. Hope that cleared up all the confusion!
Answer by andaleeni · Feb 21, 2012 at 10:21 PM
Yes that sounds like a good solution muzz id be very greatful if you could write me an example! Thanks so much for the reply!
Your answer
Follow this Question
Related Questions
Networked Audio Listeners 2 Answers
sound is pitched down unexpectable 1 Answer
Separate Listeners for Sound FX and Music? 0 Answers
How to change pitch or volume on ALL audio sources in scene? 4 Answers