如何让音频源播放,播放完毕后播放器检测器销毁?

How do I make an audio source play and then when finished the player detector destroys?

你好,我在 Unity 中有一个 2d Platformer,我有一个带有盒子碰撞器的精灵,它检测它是否与播放器接触,当它接触到时它应该播放音频源,当音频源是时暂停它完成它应该恢复时间并摧毁探测器。我已经尝试过 Enumerators,但它不会暂停时间,检测器也不会停止工作。

代码如下:

public AudioSource UFOTALK1;

public GameObject player;

public BoxCollider2D sp;

bool used = false;

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        if (!used)
        {
            waiter();
            UFOTALK1.Play();
            used = true;
        }
        else
        {

        }
        
        
    }
}



void PauseGame()
{
    player.GetComponent<PlayerMovement>().enabled = false;
}

void ResumeGame()
{
    player.GetComponent<PlayerMovement>().enabled = true;
}

IEnumerator waiter()
{
    PauseGame();
    

    
    yield return new WaitForSeconds(7.392f);
    sp.enabled = false;
    ResumeGame();

    
    

}

}

您必须 运行 使用 StartCoroutine

的例程
private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        if (!used)
        {
            StartCoroutine (waiter());

            UFOTALK1.Play();
            used = true;
        }   
    }
}

实际上为了不必硬编码,您可以直接等到完整的音频剪辑使用完

yield return new WaitForSeconds (UFOTALK.clip.length);