Repeat audio clip in Unity, with increasingly smaller intervals

两盒软妹~` 提交于 2019-12-13 16:20:16

问题


I want a countdown timer to tick faster as the time counts down. the 'tick' is a very short audioclip.

I've got the timer working which calls playTickManager() every frame.

However with this implementation, at some transitions of cancel/re-invoke, the gap between ticks is inconsistent or overlaps causing an unnatural sounding tick.

Is there a better way to implement this without speeding up the actual audioclip, thus maintaining pitch etc?

// This is called every frame by the 'timer' script as it counts down to 0. 
// 'seconds' is a float.

void playTickManager()
{
    Debug.Log(seconds);
    if (seconds >= 20 && tickPlaySpeed != 1)
    {
        InvokeRepeating("playTickAudio", 1f, 1f);
        tickPlaySpeed = 1;
    }
    else if (seconds >= 16 && seconds < 20 && tickPlaySpeed != 2)
    {
        CancelInvoke("playTickAudio");
        InvokeRepeating("playTickAudio", 0f, .85f);
        tickPlaySpeed = 2;
    }
    else if (seconds >= 12 && seconds < 16 && tickPlaySpeed != 3)
    {
        CancelInvoke("playTickAudio");
        InvokeRepeating("playTickAudio", 0f, .7f);
        tickPlaySpeed = 3;
    }
    else if (seconds > 8 && seconds < 12 && tickPlaySpeed != 4)
    {
        CancelInvoke("playTickAudio");
        InvokeRepeating("playTickAudio", 0f, .55f);
        tickPlaySpeed = 4;
    }
    else if (seconds >= 4 && seconds < 8 && tickPlaySpeed != 5)
    {
        CancelInvoke("playTickAudio");
        InvokeRepeating("playTickAudio", 0f, .4f);
        tickPlaySpeed = 5;
    }
    else if (seconds > 0 && seconds < 3 && tickPlaySpeed != 6)
    {
        CancelInvoke("playTickAudio");
        InvokeRepeating("playTickAudio", 0f, .25f);
        tickPlaySpeed = 6;
    }
    else if(seconds <= 0)
    {
        CancelInvoke("playTickAudio");
        tickPlaySpeed = 0;
    }
}

void playTickAudio()
{
    AudioSource.PlayClipAtPoint(tickAudio, gameObject.transform.position, 1f);
}

回答1:


This is a good chance to learn a very common pattern.

This is the "call yourself again" pattern in software.

Note that this is very, very common in games - notably every type of weapon uses the "call yourself again" pattern. (In short, you don't use something like "invokeRepeating" since you need more control over the firing loop.)

So try this ... teaching example:

private float gapToNext;

private void ExcitingCountdown()
  {
  gapToNext = 2.5f;
  PlayOneStep();
  }

private void PlayOneStep()
  {
  if (gapToNext < 0.01f) return;
  YourBeepSound.Play();
  gapToNext = gapToNext - 0.1f;
  Invoke("ExcitingCountdown", gapToNext);
  }

Simple right?

Life is easier when you know the patterns :)

Note that in reality, your code would look like this:

Just adjust the values in ExcitingCountdown to get the sounding result you want.

private float gapToNext;
private int safetyCounter;

void Start()
  {
  ExcitingCountdown();
  }

private void ExcitingCountdown()
  {
  gapToNext = 2.5f;
  safetyCounter = 30;
  PlayOneStep();
  }

private void PlayOneStep()
  {
  if (gapToNext < 0.05f || safetyCounter<0)
    {
    Debug.Log("we're done...!");
    return;
    }

  YourBeepSound.Play();

  gapToNext = gapToNext * 0.85f;
  --safetyCounter;

  Debug.Log("gap is now " +gapToNext.ToString("f4"));

  Invoke("ExcitingCountdown", gapToNext);
  }


来源:https://stackoverflow.com/questions/37944083/repeat-audio-clip-in-unity-with-increasingly-smaller-intervals

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!