Unity3d Unity中的audio.Play()和Destroy(游戏对象)计时问题

Unity3d Unity中的audio.Play()和Destroy(游戏对象)计时问题,unity3d,playback,destroy,Unity3d,Playback,Destroy,我已经为我的明星做了剧本,给1分,制造粒子,播放声音,摧毁物体。一切正常,但现在脚本等待声音停止播放。在播放声音之前,你建议用什么方法来破坏这个物体? 我可以制作新的gameobject音频源预制,将其连接到star的群中,并像使用粒子系统一样调用播放,或者有更好的方法来实现这一点吗 using UnityEngine; using System.Collections; public class Star : MonoBehaviour { public ParticleSyst

我已经为我的明星做了剧本,给1分,制造粒子,播放声音,摧毁物体。一切正常,但现在脚本等待声音停止播放。在播放声音之前,你建议用什么方法来破坏这个物体? 我可以制作新的gameobject音频源预制,将其连接到star的群中,并像使用粒子系统一样调用播放,或者有更好的方法来实现这一点吗

using UnityEngine;
using System.Collections;


public class Star : MonoBehaviour {

    public ParticleSystem StarParticle;

    public AudioClip otherClip;

    IEnumerator OnTriggerEnter (Collider player)
    { 
        ScoreManager.score += 1;


        StarParticle.Play ();

        AudioSource audio = GetComponent<AudioSource>();
        audio.Play();
        yield return new WaitForSeconds(audio.clip.length);

        Destroy (gameObject);

    }
}
使用UnityEngine;
使用系统集合;
公务舱明星:单一行为{
公共粒子系统;
公共音频剪辑;
IEnumerator OnTriggerEnter(对撞机播放器)
{ 
ScoreManager.score+=1;
Play();
AudioSource audio=GetComponent();
音频播放();
返回新的WaitForSeconds(audio.clip.length);
摧毁(游戏对象);
}
}

这就是代码。

从阅读评论中可以看出:

audio.clip.length
的值放入一个变量中,并将其减小,例如除以2或4等,以便脚本在音频文件播放过程中继续播放50%或25%

 IEnumerator OnTriggerEnter (Collider player)
{ 
    ScoreManager.score += 1;


    StarParticle.Play ();

    AudioSource audio = GetComponent<AudioSource>();

    waitValue = (audio.clip.length/2);
    audio.Play();
    yield return new WaitForSeconds(waitValue);

    Destroy (gameObject);

    }
IEnumerator OnTriggerEnter(对撞机播放器)
{ 
ScoreManager.score+=1;
Play();
AudioSource audio=GetComponent();
waitValue=(audio.clip.length/2);
音频播放();
返回新的WaitForSeconds(waitValue);
摧毁(游戏对象);
}

这就是我问题的解决方案。 我禁用collider(不允许其他ppl从该恒星获取点)和mesh(使恒星不可见),然后在播放后销毁我的恒星游戏对象。谢谢回复

使用UnityEngine; 使用系统集合

公务舱明星:单一行为{

public ParticleSystem StarParticle;

IEnumerator OnTriggerEnter (Collider player)
{ 
    ScoreManager.score += 1;


    StarParticle.Play ();

    AudioSource audio = GetComponent<AudioSource>();
    audio.Play();
    GetComponent<Collider>().enabled = false;
    GetComponent<MeshRenderer>().enabled = false;
    yield return new WaitForSeconds(audio.clip.length);

    Destroy (gameObject);

}
公共粒子系统StarParticle;
IEnumerator OnTriggerEnter(对撞机播放器)
{ 
ScoreManager.score+=1;
Play();
AudioSource audio=GetComponent();
音频播放();
GetComponent().enabled=false;
GetComponent().enabled=false;
返回新的WaitForSeconds(audio.clip.length);
摧毁(游戏对象);
}

}

那么,收益率回报率在做什么?如果您在运行脚本时注释掉了该行,这不会解决您的问题吗?我想象那会播放声音,在不等待声音播放完成的情况下销毁对象。如果没有收益,游戏对象在声音播放之前被销毁,因为它动作非常快。这是我的问题,如何在不等待几秒钟的情况下在触发器enter上删除项,但让声音播放到最长的时间。下面是我的anwser:但是你是说对象一被删除,声音就停止了吗?我自己对Unity是相当陌生的,我的语法也不完善,RE:变量声明,等等。对不起。除以2或4不是一个选项。它以半隐格式剪切音频,不是播放音频的时间段,而是值,因此如果音频文件为4秒,则值为2秒,因此游戏过程在音频开始后等待2秒,然后销毁对象。如果移除对象也会切断相关音频,我会感到惊讶,除非音频是对象的子对象或组件?在哪种情况下,将音频作为一个单独的对象?这有帮助吗?这就是为什么我等了几秒钟。如果音频源是一个对象的元素,它会随着他一起被破坏。