Skip to content

[Unity] Pathing volume not reset on discontinuous position change #549

Description

@xcerv

System Information

Please provide the following information about your system:

  • Steam Audio version: 4.8.1
  • Unity version: Unity 6.4 URP, 6000.4.2f1
  • Operating System and version: Windows 10
  • CPU architecture: x86-64

Issue Description

Pathing volume doesn't get re-initialized when audio source is discontinuously moved and started beyond the visibility of the listener. Instead, the last volume level prior to the sound being moved is used.

This becomes problematic when utilising Object Pooling with the setup of GameObject prefab with AudioSource + SteamAudioSource, where the sound is moved to the desired position, the SteamAudioSource settings are initialized and then the AudioClip is played. When moving the sound back to the pool, the SteamAudioSource component is disabled as well as the entire instance - and later enabled only right before when playing the new sound.

Steps To Reproduce

Here are my current settings related to Steam Audio:
Image
Image
Image

Note: I enable the SteamAudioSource component prior to running AudioSource.Play(), and similarly set the Spatial Blend only before playing the sound.

Steps to reproduce the behavior:

  1. Create a scene with a plane for floor and a wall
  2. Add the GameObject with the AudioSource and SteamAudioSource components (using settings above) on one side of the wall
  3. Place the MainCamera with the SteamAudioListener (apply reverb, baked) on the other side of the wall
  4. Add a ProbeBatch and bake it, as well as the reverb
  5. Start the Game
  6. In the Scene view, move the AudioSource quickly away from MainCamera, so that the wall remains between the sound and the MainCamera, occluding the AudioSource.
  7. The pathing volume doesn't get updated and stays at the same level as it did when close to the wall. It gets updated only once the MainCamera/listener is visible again.

Alternative Scenario
In my specific case, I encounter these problems when I use single 'Main Scene', which gets a new 'Subscene A' loaded in additively. Both 'Main Scene' and 'Subscene A' have their own ProbeBatches (baked separately, with each of the scenes loaded in the Editor in single-mode prior to the bake). If the MainCamera is still present in 'Main Scene' (and its probe batch) when the 'Subscene A' loads, and the audio effects start in the 'Subscene A' after it is loaded, then upon entering the 'Subscene A' and enabling pathing for the previously started sounds (and settings their probe batch to the 'Subscene A'), the volume of these sounds doesn't match their position - certain sounds are much louder than they should be, until the MainCamera is moved, so there's clear visibility of the AudioSource (or the source is 'nudged' and moved in the scene editor).

The exact steps for this scenario are:

  1. Main Scene loads with 'ProbeBatchMain'
  2. Subscene A loads additively with 'ProbeBatchA'
  3. A SoundEffect 'TestSound' prefab is taken from Object Pool (it has AudioSource and SteamAudioSource components); it is first moved to the desired position, the steam audio settings are assigned (temporarily pathing = false, probebatch = null) and the AudioSource.Play() is called
  4. The MainCamera enters 'ProbeBatchA', the 'TestSound' has its pathing = true; probebatch = 'ProbeBatchA'
  5. The pathing volume of the 'TestSound' doesn't correspond to its current position (e.g. fully loud; disabling pathing fixes it)
  6. If MainCamera moves to see the 'TestSound', the volume corrects itself

Object Pooling
As for how the Object Pooling works:

  1. The SoundEmitter prefab has AudioSource and SteamAudioSource components, with the settings described above
  2. When first created, the SoundEmitter is instantiated and the gameObject is immediately disabled:
 private SoundEmitter SoundEmitterConstructor()
{
    var soundEmitter = Instantiate(soundEmitterPrefab);
    soundEmitter.gameObject.SetActive(false);
    return soundEmitter;
}
  1. When taken from the pool, the gameObject is enabled: soundEmitter.gameObject.SetActive(true);
  2. Then the position of the SoundEmitter is set at desired position using transform.position
  3. AudioClip is assigned as well as the SteamAudio properties (see the settings above)
  4. If contained within the same probe batch as the MainCamera (with SteamAudioListener), the pathing is set to true and pathingProbeBatch set to the ProbeBatch the MainCamera is inside of; otherwise pathing is false and probeBatch null
  5. Only then the component steamAudioSource = enabled
  6. AudioSource.Play() is called
  7. When sound stops playing, the AudioSource.Stop() is called, the steamAudioSource.enabled = false and the emitter is returned to pool, causing the SoundEmitter.gameObject.SetActive(false) to be called

Workaround
When switching active probe batches, or moving the SoundEffect/MainCamera discontinuously, the following code is executed on the SteamAudioSource:

  public void RecreateSource()
  {
      // Clear old source
      SteamAudioManager.RemoveSource(this);
      mSource.RemoveFromSimulator(mSimulator);
      mSource.Release();
      mSource = null;

      // Create new handle
      var settings = SteamAudioManager.GetSimulationSettings(false);
      mSource = new Source(SteamAudioManager.Simulator, settings);

      // Init handle with source
      mAudioEngineSource.Initialize(gameObject);
      mAudioEngineSource.UpdateParameters(this);
      
      // Re-register with simulator
      mSource.AddToSimulator(mSimulator);
      SteamAudioManager.AddSource(this);
  }

As this seems to re-initialize the pathing volume. However, it might be preferable to utilise dedicated API.

Minimal Project
Unity Package link: https://drive.google.com/file/d/1Jl3vMcw1fhUkaI3WIYx_x0Z272w-s0Th/view?usp=sharing
Contains the scene and all required dependancies/settings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions