【问题标题】:How do I make an audio source play and then when finished the player detector destroys?如何播放音频源,然后播放器检测器在完成后销毁?
【发布时间】:2020-12-23 21:14:34
【问题描述】:

你好,我在 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();

    
    

}

}

【问题讨论】:

    标签: c# unity3d audio-source


    【解决方案1】:

    您必须使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-06
      • 2018-03-21
      • 2022-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多