SFE_Sound.js 782 B

123456789101112131415161718192021222324252627282930313233
  1. //this script plays a sound. The trick is that the sound stays played even if the object is destroyed while playing the script.
  2. //the idea is that the script generates a temporarly object that plays the sound.
  3. #pragma strict
  4. var soundEffect:AudioClip;
  5. var minPitch:float=0.9;
  6. var maxPitch:float=1.1;
  7. function Start () {
  8. PlayClipAt(soundEffect, transform.position);
  9. }
  10. function Update () {
  11. }
  12. function PlayClipAt(clip: AudioClip, pos: Vector3): AudioSource {
  13. var tempGO = GameObject("TempAudio");
  14. tempGO.transform.position = pos;
  15. var aSource = tempGO.AddComponent(AudioSource);
  16. aSource.clip = soundEffect;
  17. aSource.pitch=Random.Range(minPitch, maxPitch);
  18. // soundEffect.pitch = 1.5;
  19. aSource.Play();
  20. Destroy(tempGO, clip.length);
  21. return aSource;
  22. }