Advertisement
salahzar

PlaySoundWhenEnter.js

Nov 14th, 2021
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. print ("Script Entity Enter Loaded");
  2. (function () {
  3.     // position Vec3 (Vec3.ZERO)
  4.     // orientation Quaternion (Quaternion.IDENTITY)
  5.     // volume Number 1.0
  6.     // pitch 0.0625 – 16.0   1 no change in pitch min +- 2 octaves
  7.     // loop boolean false
  8.     // secondOffset number 0.0
  9.     // localOnly boolean false
  10.     // { "soundURL":"http","position": Vec3.ZERO, "volume": 1.0, "pitch": 1.0, "loop": false, "secondOffset": 0.0, "localOnly": false }
  11.     var soundURL = null;//SoundCache.getSound(SOUND_URL);
  12.     var lastSoundURL = null;
  13.     var soundVolume = null;
  14.     var soundData = null;
  15.     var pitch = null;
  16.     var injector = null;
  17.     var properties = null;
  18.  
  19.  
  20.     this.leaveEntity = function (entityID) {
  21.         print("Entity Leaving bounding box");
  22.     }
  23.     this.enterEntity = function (entityID) {
  24.         print("Entity Entered in bounding box");
  25.         properties = Entities.getEntityProperties(entityID, ["position", "userData"]);
  26.         if (!properties.userData) {
  27.             print("Click Sound emitter " + entityID + " missing user data.");
  28.             return;
  29.         } try {
  30.             soundData = JSON.parse(properties.userData);
  31.             print("SoundURL " + soundData.soundURL);
  32.             // need to check that all this stuff even exists and throw error if not.s
  33.             soundURL = SoundCache.getSound(soundData.soundURL);
  34.             //receiverName = soundData.receiverName;
  35.             soundVolume = !isNaN(soundData.soundVolume) ? Number(soundData.soundVolume) : 0.0;
  36.             soundLoop = soundData.isLoop;
  37.             soundLocal = soundData.isLocal;
  38.             pitch = soundData.pitch;
  39.         } catch (e) { }
  40.  
  41.  
  42.         injector = Audio.playSound(soundURL, {
  43.             position: properties.position,
  44.             volume: soundVolume,
  45.             loop: soundLoop,
  46.             localOnly: soundLocal,
  47.             pitch: pitch
  48.         });
  49.  
  50.     }
  51.     this.unload = function () {
  52.         if (injector) {
  53.             injector.stop();
  54.             injector = null;
  55.         }
  56.     };
  57. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement