Multiple AudioSources, and fading sounds

So, I had my sound all nicely configured before this lecture, where my thrust sound faded in as I pressed Space, and faded out when I released it. Then I had to add in other AudioClips that ran as OneShots. This is my solution. I’d be interested to hear if there’s an easier way:

I have two AudioSources on my player, one of them has the thrust noise set in the inspector, the sounds for the second one are set via PlayOneShot();
I’ve removed the irrelevant bits of code to only show the audio related parts.

AudioSource flyingSound;
AudioSource soundEffectSounds;

[SerializeField] private int FadeInTime; // The time it takes for the audio to fade in.
[SerializeField] AudioClip HurtNoise;
[SerializeField] AudioClip VictoryNoise;

// Use this for initialization
void Start ()
{
    AudioSource[] Sounds = GetComponents<AudioSource>();
    flyingSound = Sounds[0]; // Already has AudioClip defined in inspector.
    soundEffectSounds = Sounds[1];

    flyingSound.volume = 0; // I want thrusters sound to not be heard
    flyingSound.Play(); // I want the audio to loop constantly, and fade in/out when needed
}

// Update is called once per frame
void Update ()
{
    if (state == State.Alive)
    {
        RespondToRotateInput();
        RespondToThrustInput();
    }
    else FadeOut(); // Fade out thrust sound if not alive.

}

void OnCollisionEnter(Collision collision)
{
    if (state != State.Alive)
    {
        return;
    }
    switch (collision.gameObject.tag)
    {
        case "Friendly":
            break;
        case "Finish":
            StartSuccessSequence();
            break;
        default:
            StartDeathSequence();
            break;

    }
}

private void StartSuccessSequence()
{
    state = State.Transcending;
    soundEffectSounds.PlayOneShot(VictoryNoise);
    Invoke("LoadNextLevel", 1f); // TODO chance time parameter to a variable
}

private void StartDeathSequence()
{
    print("you died");
    state = State.Dying;
    soundEffectSounds.PlayOneShot(HurtNoise);
    Invoke("LoadFirstLevel", 1f);
}

private void RespondToThrustInput()
{
    if (Input.GetKey(KeyCode.Space))
    {
        float thrustThisFrame = thrustFactor * Time.deltaTime;
        rigidBody.AddRelativeForce(Vector3.up * thrustThisFrame);
        FadeIn(); // If space is pressed, fade in thrust audio
    }
    else
    {
        FadeOut(); // If not thrusting, fade out thrust audio
    }
}

private void FadeIn()
{
    if (flyingSound.volume < 1)
    {
        flyingSound.volume = flyingSound.volume + (Time.deltaTime / (FadeInTime + 1));
    }
}

private void FadeOut()
{
    flyingSound.volume = flyingSound.volume - (Time.deltaTime / (FadeInTime + 1));
}

}

Hi, I came up with something similar in my mind when I was thinking how I can play Success sound while still playing and stopping engine sound according to thrust key event. I’m not right person to answer if your solution is the best way but just wanted to say I did think about similar thing. One thing that I’m worried about is referencing audio sources by index though. It works yes but the day will come when someone will add another audio source at front… Well probably not but in “real life” that might be issue. Or Unity will be updated and those audio sources will be in reverse order. But yeah, kudos to you going extra mile! :slightly_smiling_face:

Privacy & Terms