How to apply two audio files to an object?

I’ve got an audio file attached to an object that plays when that object collides above a certain relative velocity threshhold. What I would like to do is to change it so that above a certain relative velocity one sound plays and below that relative velocity another sound plays.

My questions are: Can I apply more than one audio file to an object? If so, how do I call each audio file separately?

Is something like this possible [PSEUDOCODE]?

function OnCollisionEnter (col : Collision) { 
   if (col.relativeVelocity.magnitude > 1.0) {
      audio.one.Play ();
      }
   else {
      audio.two.Play ();
      }
   }

This is almost straight from memory, but I think its correct:

I used the GameObject’s audio.clip property to change the sound with reasonable effect.

Just store the AudioClips you want to use as variables in the script attached to the GO and drag them in the inspector.

AudioClip bigBang;
AudioClip softBang;

function OnCollisionEnter (col:Collision)
{
  if (col.relativeVelocity.magnitude > 1.0)
  {
      audio.clip = bigBang;
      audio.Play ();
  }
  else 
  {
      audio.clip = softBang;
      audio.Play ();
  }
}

Thanks Lurid, that was pretty close. This worked:

var bigBang : AudioClip;
var softBang : AudioClip;

function OnCollisionEnter (col:Collision)
{
  if (col.relativeVelocity.magnitude > 1.0)
  {
      audio.clip = bigBang;
      audio.Play ();
  }
  else
  {
      audio.clip = softBang;
      audio.Play ();
  }
}

That’s a much more friendly way to remind me about var declarations than the compiler does when I switch between C# and JavaScript too often :lol:

That’s an interesting solution. Last time I asked that question (about multiple audio souces and a singe object), I was told I couldn’t do it and that I had to create a dummy object and attach the audio to that. Eventhough I don’t really understand the code, I get the concept.

Would you make that snippet available on the WIKI?

The one possible caveat (that I don’t recall for sure) is that a sound already playing this way might be interrupted if the audio clip is changed out from underneath it. I believe it will be interrupted, just don’t have a quick way to verify…

If so, there are certainly ways around it. For example you could instantiate a temporary game object at the location that plays the sound at the proper location, and then destroys itself. The object could even be attached to the original object if you need the sound to keep moving around.

After a bit of testing, I can’t tell one way or the other. My sound clips are pretty short (about 1.5 seconds) so it doesn’t seem to effect it.

One interesting thing I learned though: The volume level set on the audio source seems to effect both audio clips.