Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question AudioSource.PlayOneShot with negative pitch won't start new sounds

Discussion in 'Scripting' started by joelarmst, Oct 10, 2021.

  1. joelarmst

    joelarmst

    Joined:
    Apr 21, 2018
    Posts:
    2
    This is either a bug or there is something I could be doing differently. I have a game that is heavily dependent on environment sound effects that use PlayOneShot(). I allow the player to rewind time in this game; while these sound effects have a non-zero play position, I hear them played in reverse if time/pitch is negative. The sound effects I have that use Play() and have looping enabled are also played in reverse correctly.

    However, after a few seconds of rewinding time, none of the audio sources using PlayOneShot() will start a new sound effect playthrough. It becomes completely silent instead of being able to play those effects starting from the end, even though the code is definitely replaying everything as it should.

    Here's a sample of my code (at least with everything I've tried so far) the moment before audio is supposed to be triggered. ChangePitch() typically happens when the player rewinds but there's no difference if I uncomment that line. I have also tried with/without setting the .time and .timeSamples fields, no difference in behavior.

    Code (CSharp):
    1. if (_currentTimeMultiplier < 0 && RefAudioSource.timeSamples <= SamplesFromEndBeforeReset)
    2. {
    3.     // set time position manually to make sure one shot plays correctly
    4.     RefAudioSource.timeSamples = RefAudioSource.clip.samples - 1;
    5.     RefAudioSource.time = RefAudioSource.clip.length;
    6. }
    7. else if (_currentTimeMultiplier > 0 && RefAudioSource.timeSamples > 0 && RefAudioSource.timeSamples >= Math.Max(0, RefAudioSource.clip.samples - 1 - SamplesFromEndBeforeReset))
    8. {
    9.     RefAudioSource.timeSamples = 0;
    10.     RefAudioSource.time = 0;
    11. }
    12.  
    13. // make sure pitch is set
    14. //ChangePitch(_currentTimeMultiplier);
    15.  
    16. RefAudioSource.PlayOneShotIfNotNullActiveAndEnabled(); // don't provide volume; audiosource should have volume scaled already
     
  2. FOXAcemond

    FOXAcemond

    Joined:
    Jan 23, 2015
    Posts:
    99
    Hey I'm also stumbling on that. Did you find a solution with
    PlayOneShot
    or did you go with
    Play
    ?