Search Unity

Audio help!

Discussion in 'Scripting' started by xacto, Aug 23, 2005.

  1. xacto

    xacto

    Joined:
    Jul 20, 2005
    Posts:
    112
    I know how to play a single audio with the following code and by dragging the soundfile onto the object... but how do I play multiple sounds. Ex: a winning sound, losing sound.

    Can I script the code like: audioPlay(winSound) or audioPlay("loseSound)? I could not get it to work. (just one sound per obect.)

    Thanks!


    Code (csharp):
    1. if (col.relativeVelocity.magnitude > 6) {
    2.         if (audio  !audio.isPlaying) {
    3.             audio.Play ();
    4.         }
    5.     }
    6.  
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    I assume you've read the docs on scripting Audio Sources (http://www.otee.dk/Documentation/ScriptReference/AudioSource.html )

    You can use the PlayOneShot function, which takes an AudioClip as the first argument. Just put a list of variables into your script requiring an AudioClip and then use them as the argument to PlayOneShot.

    Code (csharp):
    1.  
    2. var winningSound : AudioClip;
    3. var losingSound : AudioClip;
    4.  
    5. // etc.
    6.  
    You should also be able to assign a new value to the clip attribute of the AudioSource from scripting.
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    What we did in GooBall was to have many audio sources with a clip each. Then we could simply fade each one in and out... E.g. we had 3 ball rolling sounds, then we crossfaded them depending on ball speed. If the player was not in ground contact, we would fade in an air swoosh over .3 seconds and fade out the rolling sounds over .1 (to avoid clicking)....

    Was a bit weird to set up, but worked great...
     
  4. xacto

    xacto

    Joined:
    Jul 20, 2005
    Posts:
    112
    ooo that sounds interesting... will have to give that a try as well.

    Thanks everyone!