Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Audio coroutine?

Discussion in 'Editor & General Support' started by polytropoi, Sep 11, 2007.

  1. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    I'm trying to make a triggered sound loop properly:

    Code (csharp):
    1.  
    2. var rsound : AudioClip;
    3. function Start () {
    4.        
    5.     while (Input.GetButton("Rocket")) {
    6.     audio.clip = rsound;
    7.     audio.Play ();
    8.     yield WaitForSeconds (audio.clip.length);
    9.  
    10.     }
    11. }
    12.  
    It's looping when the button's down, but not yielding.

    I couldn't figure out how to use Update with StartCoroutine - is this necessary?
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Code (csharp):
    1. var rsound : AudioClip;
    2. function Start ()
    3. {
    4.      while (true)
    5.      {
    6.            if (Input.GetButton ("Rocket"))
    7.            {
    8.                 audio.clip = rsound;
    9.                 audio.Play ();
    10.                 yield WaitForSeconds (audio.clip.length);
    11.            }
    12.            else yield;
    13.      }
    14. }
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I don't think Start and Update support the use of coroutines so why not break things up a bit. Here's some code I just tested with a simple gun-fire sound and all worked like a charm:

    Code (csharp):
    1. var gunsound : AudioClip;
    2. var playing = false;
    3.  
    4. function Update () {
    5.      
    6.   if (Input.GetKey("space")) {
    7.     if (!playing) {
    8.       playing = true;
    9.       GunFire();
    10.     }
    11.   } else {
    12.     playing = false;
    13.   }
    14.    
    15. }
    16.  
    17. function GunFire () {
    18.    
    19.   audio.clip = gunsound;
    20.    
    21.   while (playing) {
    22.     audio.Play ();
    23.     yield WaitForSeconds (audio.clip.length);    
    24.   }
    25.    
    26. }
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,749
    As a side note, Start does support coroutines. It's only Update/LateUpdate that don't, I don't think. (Probably FixedUpdate too?)
     
  5. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Yup, I knew I was off in my immediate recollections so thanks for that correction.
     
  6. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    Thanks gents - these approaches both work, but there's a wait cycle equal to the length of the clip after it loops each time. I was looking for a way to hold down a key and get a seamless loop while the key or button is down, and an instant cutoff the the key is released...

    So instead of using audio.clip.length, I manually tweakd the WaitForSeconds value to a fraction of the total clip length i.e. yield WaitForSeconds (.2), and it loops seamlessly, although with that technique it overplays by that time value. Not really a problem, but...
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. var rsound : AudioClip;
    2.  
    3. function Start() {
    4.     audio.clip = rsound;
    5.     audio.Play();
    6.     while (Input.GetButton("Rocket")) {
    7.         yield;
    8.     }
    9.     audio.Stop();
    10. }
    Just make sure the audio source is set to "Loop". If you can use "Play on awake", then leave out audio.Play(). If you're just using one sound, assign that in the inspector and leave out the lines with the "rsound" variable.

    --Eric
     
  8. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    Thanks Eric, but that one's no workie. Once it hits stop, how is it supposed to loop, since it's function Start()?
     
  9. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    yield

    :)

    It goes into a loop that yields (waits one frame) over and over until the button is released, just like you asked for. Then it will get to the end when you let go.

    If you tried it, maybe you don't have your button set up correctly? Try it with your previous spacebar for the weapon. Should work fine, by my estimation.
     
  10. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    Nope, sorry - I tried GetKey and GetButton, no sound.

    I think understand the concept, but when it hits stop once (when it initializes, since the key isn't down) what would make it start again, since it's not in an Update?

    I attached a test project w/ Eric's script - am I overlooking something? Help appreciated. This is one of those deceptively simple things that confounds me sometimes...
     

    Attached Files:

  11. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Try this:

    Code (csharp):
    1. var rsound : AudioClip;
    2.  
    3. function Update ()
    4. {
    5.     if (Input.GetButtonDown ("Rocket"))
    6.     {
    7.         if (!audio.isPlaying)
    8.         {
    9.             audio.loop = true;
    10.             audio.Play ();
    11.         }
    12.     }
    13.     else
    14.     {
    15.         if (Input.GetButtonUp ("Rocket"))
    16.         {
    17.             audio.loop = false;
    18.             audio.Stop ();
    19.         }
    20.     }
    21. }
     
  12. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    681
    That's it! Thanks Daniel! Nice solution.
     
  13. aaronsullivan

    aaronsullivan

    Joined:
    Nov 10, 2005
    Posts:
    986
    Heh, yup. Didn't think of starting it, just continuing it.
     
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sorry, I thought you only wanted it to be a one-time thing, since you had it in a Start function. :)

    --Eric