Solution for annoying clipping sound on audio stop! :)

image

5 Likes

Thank you. That ciipping sound was really annoying. Seems like not everyone was experiencing it?

Bloody legend! I was trying to solve this clicking sound problem for the better part of a day.

Hell yeah, super simple to implement and EXACTLY what I was looking for. Thank you for sharing! :smiley:

Wow! Thank you so much, I was trying to find something like this on the internet for so long. The videos take almost 20 mins to explains something as simple as this!

Thanks for the solution! I wasn’t sure I was going to find a way to fix it by Googling

Hey, I like this! Just one tip or preference to add; If you use an if statement over a while you can achieve a proper fadeout. I also changed the -= value to 0.2f so it would read as follows;

void FadeOut()
{
if (audioSource.volume > 0)
{
audioSource.volume -= 0.2f * Time.deltaTime;
}
}

try it out!

1 Like

Very nice addition thanks :slight_smile:

Nice! Just a side note - you can use the isPlaying property of AudioSource instead of a private boolean (soundPlaying). Also you can reset the volume to the initial volume with another variable instead of a hard-coded value.

Here’s my solution:

public class Rocket : MonoBehaviour
{
    Rigidbody rigidBody;
    AudioSource audioSource;

    float thrust = 120;
    float rotationRate = 30;

    float audioStartVolume = 0.0f;
    float audioFadeRate = 0.5f;

    // Use this for initialization
    void Start ()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
        audioStartVolume = audioSource.volume;
    }
	
    // Update is called once per frame
    void Update ()
    {
        ProcessInput();
    }

    private void ProcessInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            rigidBody.AddRelativeForce(Vector3.up * Time.deltaTime * thrust);

            if (!audioSource.isPlaying)
            {
                audioSource.Play();
            }

            audioSource.volume = audioStartVolume;
        }
        else
        {
             FadeAudioOut();
        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * Time.deltaTime * rotationRate);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(Vector3.forward * Time.deltaTime * -rotationRate);
        }
    }

    private void FadeAudioOut()
    {
        if (audioSource.volume > 0)
        {
            audioSource.volume -= audioFadeRate * Time.deltaTime;
            if (audioSource.volume <= 0)
            {
                audioSource.Stop();
            }
        }
    }

}

(I’m allowing both rotation keys to be active at the same time, if you happened to catch that ;))

Privacy & Terms