fading in and out sounds
hey everyone,
So i have this script where i play 2 sounds after each other on entercollider.
however, i want the clips to be faded in and out when entering and exiting the collider how do I go about that?
this is my code untill now
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class fadeforcouple : MonoBehaviour {
public AudioSource source;
public AudioSource source2;
public AudioClip goaway;
public AudioClip shoo;
private IEnumerator PlaySounds ()
{
// Play the first sound
AudioSource.PlayClipAtPoint(goaway, transform.position);
//Then wait 3 seconds and play the other
yield return new WaitForSeconds(7);
AudioSource.PlayClipAtPoint(shoo, transform.position);
}
private void OnTriggerEnter ()
{
StartCoroutine(PlaySounds());
}
private void OnTriggerExit ()
{
StopCoroutine(PlaySounds());
}
}
I tried from a youtube tutorial like
IEnumerator fadesound() while(source.volume > 0.01f) { source.volume =- Time.deltatime/ 1.0f; yield return null;
{ source.volume = 0; source.stop ();
And i stumble upon quite a lot of posts related to this but im not that good in program$$anonymous$$g yet and so i dont understand how to apply it to my own code...
Answer by linelade · Aug 27, 2017 at 11:25 AM
something like this, but im quite noob and dont really get what to call in the trigger enter and exit?
public AudioSource source;
public AudioSource source2;
public AudioClip goaway;
public AudioClip shoo;
private IEnumerator PlaySounds ()
{
AudioSource.PlayClipAtPoint(goaway, transform.position);
yield return new WaitForSeconds(7);
AudioSource.PlayClipAtPoint(shoo, transform.position);
}
IEnumerator FadeAudio(bool fadeIn, float fadeSeconds = 1)
{
for (float i = 0; i < 1; i += Time.deltaTime / fadeSeconds)
{
AudioSource.volume = fadeIn ? i : 1 - i;
yield return null;
}
}
private void OnTriggerEnter ()
{
StartCoroutine(PlaySounds());
}
IEnumerator OnTriggerExit ()
{
StopCoroutine(PlaySounds());
yield break;
}
}
Your answer
Follow this Question
Related Questions
How do you add audio to Audio Clip (via scripting) during gameplay? 1 Answer
Audio play when enter trigger 1 Answer
Audio on keydown Won't stop 1 Answer
Trying to make a splitscreen game, having issues with the AudioSource and distance between objects. 0 Answers
Recording Players voice in Unity using VR buttons. 0 Answers