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();
}
}
}