Audio effects

The prefab I chose to use for the AOE was from a really nice RPG FX pack. It had an audio effect on the prefab. It trails off quite a bit for a cool echo-y effect so when I try to kill the prefab it cuts out the audio abruptly. I remember from way back when that you can set the volume to 0 first to avoid this to a degree. Doing it right before the destroy did nothing… But doing this worked. Genius? Or Mad man?

    private IEnumerator Effect(AbilityData data, Action finished)
    {
        Transform prefabInstance = Instantiate(prefabToSpawn);
        prefabInstance.position = data.GetTargetedPoint();
        if(destroyDelay > 0)
        {
            yield return new WaitForSeconds(destroyDelay * 0.98f);

            AudioSource audio = prefabInstance.GetComponent<AudioSource>();
            if(audio != null) audio.volume = 0;

            yield return new WaitForSeconds(destroyDelay * 0.02f);

            Destroy(prefabInstance.gameObject);
        }
        finished();
    }

(The prefab has a custom script that handles audio playback and looping and stuff. I actually hacked it enough to where I don’t need to do that anymore, but it is a decent solution in a pinch)

Oh… I also wanted to ask why we aren’t using the overload for Destroy that takes a delay. This way we did leaves us more opportunities for other stuff… maybe?

You could use the Destroy(GameObject, float) version if you wished. Be aware though, that this sets up yet another Coroutine (Invoke and Destroy with a delay both effectively add a Coroutine with a yield return new WaitForSconds(delay).

1 Like
> Right.. it'd be the same thing that we are doing..
> 
> However, at the time of asking this I didn't realize we'd make such good use of the CoRoutine we made here for Delay Composite (and then for my most elegant, all on my own code that is my effect over time composite).

Ah… Forgot which thread this was. Ignore that above there.

Yeah this was just trying to avoid the harsh clicky sound when you destroy a GameObject that is playing audio. Even in the specific instance that got me to make this, the sound was trailing off quite nicely and already quiet… but if I destroy it early, it makes that awful very noticeable sound. Dropping the volume a fraction of a second before you destroy it makes that sound go away.

I usually use Destroy(GameObject, float) when spawning projectiles. That way, if the projectile never hits a collider, it will get destroyed anyway.

Yeah I am pretty sure the RPG course had us build that into the Projectile class.

Privacy & Terms