|
I have a spaceship object and I want to play ambient sound, engine sounds and weapon sounds from the same object. But so far I only could add empty game objects to the spaceship and setting each one as audio source and play the sounds that way. Is there an easy way to do it without creating multiple child objects?
(comments are locked)
|
|
You can attach several AudioSources to the same object in the Inspector, and get them in an array at Awake with GetComponents(AudioSource) - but knowing who's who may become a problem, since there's no component identifier.
// define the audio clips:
var clipAmb: AudioClip;
var clipEngine: AudioClip;
var clipWeapon1: AudioClip;
var clipWeapon2: AudioClip;
private var audioAmb: AudioSource;
private var audioEngine: AudioSource;
private var audioWeapon1: AudioSource;
private var audioWeapon2: AudioSource;
function AddAudio(clip:AudioClip, loop: boolean, playAwake: boolean, vol: float): AudioSource {
var newAudio = gameObject.AddComponent(AudioSource);
newAudio.clip = clip;
newAudio.loop = loop;
newAudio.playOnAwake = playAwake;
newAudio.volume = vol;
return newAudio;
}
function Awake(){
// add the necessary AudioSources:
audioAmb = AddAudio(clipAmb, true, true, 0.2);
audioEngine = AddAudio(clipEngine, true, true, 0.4);
audioWeapon1 = AddAudio(clipWeapon1, false, false, 0.8);
audioWeapon2 = AddAudio(clipWeapon2, false, false, 0.8);
}
You can then use the audio sources like this: Thanks that worked very well
Apr 15 '12 at 05:50 PM
crocodile5
(comments are locked)
|
