Monday, June 27, 2011

How do I get the Sound to play every instance

This is captured from Unity Answers
============================================

Right now, I have a custom sound clip that has a longer duration than that of the standard clip that came with the machine gun in the FPS demo. How do I make it loop on top of itself or make it play at every firing? Right now it waits for the sound file to finish before starting the next one. Here's the section of the script.
function LateUpdate() { if (muzzleFlash) { // We shot this frame, enable the muzzle flash if (m_LastFrameShot == Time.frameCount) { muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward); muzzleFlash.enabled = true;
     if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
enabled = false;

// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
Answer:
You can dynamically create and add a new audio source component and have it play the sound file and then once the sound finishes playing destroy that component.

No comments:

Post a Comment