【问题标题】:How to play sound on destroy如何在销毁时播放声音
【发布时间】:2015-01-02 05:25:13
【问题描述】:

我正在制作一个 2d 游戏,它有硬币,当玩家触摸硬币时,我试图让硬币消失并发出叮当声。问题是硬币消失但没有声音。

using UnityEngine;
using System.Collections;

public class coins : MonoBehaviour {
    static int coin = 0;
    AudioClip coinSound;
    void Start()
    {
        coin = 0;
    }

void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player")
        {
            coin++; 
            audio.PlayOneShot(coinSound);
            StartCoroutine(Ding());
            Destroy(this.gameObject);

        }
    }
    void OnDisable(){
        PlayerPrefs.SetInt ("coin", coin);

    }
    IEnumerator Ding(){
            yield return new WaitForSeconds (0.4F);
       }
}

【问题讨论】:

    标签: c# triggers unity3d gameobject


    【解决方案1】:

    你需要延迟销毁,这样你才能真正播放声音,因为它们发生在同一个对象中。

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Player")
        {
            coin++; 
            StartCoroutine(Ding());  
        }
    }
    
    IEnumerator Ding()
    {
        audio.PlayOneShot(coinSound);
        yield return new WaitForSeconds(5);
        Destroy(gameObject);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多