phaser icon indicating copy to clipboard operation
phaser copied to clipboard

audio follow does not honor the Z axis of the object

Open andreysamode opened this issue 7 months ago • 1 comments

If X and Y of the object being followed is honored, why not the Z?

Would appreciate this addition. 🙏

andreysamode avatar May 11 '25 16:05 andreysamode

Here's the diff to make it work:

diff.txt

--- WebAudioSound.js	2025-05-11 11:03:59
+++ WebAudioSound2.js	2025-05-11 10:59:39
@@ -551,7 +551,39 @@ var WebAudioSound = new Class({
     set: function (value) {
       if (this.spatialNode) {
         this.spatialNode.positionY.value = value;
+      }
+    }
+  },
+
+  /**
+   * Sets the z position of this Sound in Spatial Audio space.
+   *
+   * This only has any effect if the sound was created with a SpatialSoundConfig object.
+   *
+   * Also see the `WebAudioSoundManager.setListenerPosition` method.
+   *
+   * If you find that the sound becomes too quiet, too quickly, as it moves away from
+   * the listener, then try different `refDistance` property values when configuring
+   * the spatial sound.
+   *
+   * @name Phaser.Sound.WebAudioSound#y
+   * @type {number}
+   * @since 3.60.0
+   */
+  z: {
+
+    get: function () {
+      if (this.spatialNode) {
+        return this.spatialNode.positionZ;
+      } else {
+        return 0;
       }
+    },
+
+    set: function (value) {
+      if (this.spatialNode) {
+        this.spatialNode.positionZ.value = value;
+      }
     }
   },
 
@@ -567,12 +599,16 @@ var WebAudioSound = new Class({
     if (this.isPlaying && this.spatialSource) {
       var x = GetFastValue(this.spatialSource, 'x', null);
       var y = GetFastValue(this.spatialSource, 'y', null);
+      var z = GetFastValue(this.spatialSource, 'z', null);
 
       if (x && x !== this._spatialx) {
         this._spatialx = this.spatialNode.positionX.value = x;
       }
       if (y && y !== this._spatialy) {
         this._spatialy = this.spatialNode.positionY.value = y;
+      }
+      if (z && z !== this._spatialz) {
+        this._spatialz = this.spatialNode.positionZ.value = z;
       }
     }

andreysamode avatar May 11 '25 17:05 andreysamode