0

For the life of me I can't understand why it stops looping. I have an engine acceleration script that works great, except for the fact that it stops looping after a certain amount of time. I'm only calling the audiosource to be stopped if the user presses a button, it should work just fine. It has worked before implementing this too, I've no idea what breaks it.

private void PlayAccelerateSound()
{
    m_audioSource2.loop = true;

    m_audioSource2.clip = m_engineSound;
    if (!alreadyPlayed)
    {
        m_audioSource2.PlayOneShot(m_audioSource2.clip);
        alreadyPlayed = true;
    }

    if (rb.velocity.x < minPitch)
    {
        m_audioSource2.pitch = minPitch;
    }
    else if (rb.velocity.x > maxPitch)
    {
        m_audioSource2.pitch = maxPitch;
    }
    else
    {
        m_audioSource2.pitch = rb.velocity.x;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
alt4s
  • 97
  • 8
  • if `rb.velocity.x` is 0 then pitch will be 0. Default should be 1, so at 0 you won't hear a thing? – KYL3R Nov 07 '22 at 19:52
  • Please include a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) including enough information to reproduce the issue – derHugo Nov 07 '22 at 19:57
  • Already tried it with checking if `rb.velocity.x` equals 0, still stops looping. – alt4s Nov 07 '22 at 19:57

1 Answers1

2

Fixed it by using m_audioSource2.Play() instead of m_audioSource2.PlayOneShot(m_audioSource2.clip).

alt4s
  • 97
  • 8