In my game, I have a rolling ball with an audio source with a looping audio clip. I set it up so that the volume of that audio source is determined by the magnitude of the rigid body component of the ball. It works great, except for occasional “pops” or “clips” that occur as the ball is accelerating or decelerating. I have tried using a number of different audio clips (to determine if there was something wrong with the clip itself) but I experience these “pops” no matter what clip I use.
I am wondering if anyone has experienced this problem before and found a solution, or if there is something in my code that could be causing these “pops”. Thanks. Here is the relevant code:
private Rigidbody rb;
private AudioSource audio;
private float ballSpeed;
private float vol;
private float c = 0.015f;
void Start ()
{
rb = GetComponent<Rigidbody> ();
audio = GetComponent<AudioSource> ();
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
ballSpeed = rb.velocity.magnitude;
vol = ballSpeed * c;
audio.volume = vol;
}