- Home /
Buzzing Audio
Hello i am having problems with unity audio-source, For some reason my audio is buzzing.
There are 2 audio-sources on a tank, The Engine and Tracks. The engine plays on awake then loops then the pitch of the engine is changed in script, The tracks i want to play when (speed > 1) But for some reason the tracks still play when stationary "not intended" but the tracks audio plays and sounds normal, When i start to drive forward the tracks audio clip starts to buzz violently until my speed is at 0 again. I cannot make sense of it but i hope someone can help me. Here is the code.
EDIT: Here is a webplayer so you can see / hear the problem i am facing. Thanks.WebPlayer
public class TransformSpeed : MonoBehaviour
{
//Define the Variables
Vector3 lastPosition = Vector3.zero;
public float Speed = 0;
float maxSpeed = 25;
AudioSource tankEngine;
AudioSource tankTracks;
void Start() {
AudioSource[] audios = GetComponents<AudioSource>();
tankEngine = audios[0];
tankTracks = audios[1];
}
void FixedUpdate()
{
Speed = (transform.position - lastPosition).magnitude / Time.deltaTime * 3.6f;
//don't forget to update your lastposition after the calculation
lastPosition = transform.position;
}
void OnGUI()
{
// a simple way to display the speed in km/h of the Transform Object
GUI.Label(new Rect(10, 10, 100, 50), Speed.ToString("##0 km/h"));
if (Speed > 0){
tankEngine.audio.pitch = 0.5f + (Speed / maxSpeed);
}
if (Speed > 1){
tankTracks.Play();
}
}
}
Anybody have any rough ideas to how i can prevent this? Any information would be greatly appreciated, Thanks.
"The engine plays on awake..." This script has no Awake()
method. Is there another script that is also managing the sound?
I thnk your audio needs to be set as a 2d audio clip. Go to your Audio clip and in it inspector panel untick the box marking it as a 3D audio clip/sound. This will make it unaffected by any doppler. Could be wrong here, just sounds like you have an audio track that plays as a 3D sound in my limited experience. Cheers bud. gruffy
@$$anonymous$$elly$$anonymous$$
The engine sound plays on awake from the GameObjects AudioSource as it will always be playing that clip, So didn't need to call it from script for now.
3D Audio clips can also be unaffected by Doppler by turning down the Doppler slider in the inspector 3D Sound settings.
Hey bud, you are correct but I guess I was, more over, saying that if you need the sound to travel with you it should most oikely be set to a 2d audio clip as a matter of course. However, im basing this on best practices as I dont realky know what you are developing etcb. Anyway, thanks for getting back dude. Take care Gruffy
Answer by KellyThomas · Jan 12, 2014 at 01:34 PM
It sounds like you are starting the clip from the start every time you call AudioSource.Play()
. This would explain a buzzing when Speed
is greater than 1.
You can use `AudioSource.isPlaying` to correct this behavior. There is a nice example on that manual page.
On a side note I would recommend moving your audio logic out of OnGUI()
and into Update()
. I believe that ``OnGUI()` is called multiple times per frame, and you probably don't need to execute this logic so often.
Thanks for your help, I changed a line from
if (Speed > 1){
tankTracks.Play();
to
if (Speed > 1 && !tankTracks.isPlaying){
tankTracks.Play();
And also moved everything to Update() Thanks again.
Your answer
Follow this Question
Related Questions
Multiple audioSources on same gameObject? 1 Answer
multiple sounds, sound....sucky 0 Answers
Play sound from an array, not random, and only once 3 Answers
Intensity of sound. -1 Answers
Using Distance from AudioSource 0 Answers