淡出对象

Fade out object

如何在与一个对象发生碰撞后立即淡出?

我不能使用 Destroy (gameObject);因为那个物体是一个带有 efx 声音的硬币游戏,如果我摧毁它一旦玩家与硬币碰撞时物体声音不会播放。

如果我不销毁它,立即投币,每次碰到这个硬币你都会获得积分

我正在使用 Audio Mixer,所以真的需要来自 Audio Source 的硬币声音才能在我的设置中设置音量。

我的想法:

void OnCollisionEnter2D(Collision2D colisor)
{
    if (colisor.gameObject.tag == "Bee") {

        GetComponent<AudioSource> ().Play();

        // Here set Fade ou immediateley (idk how do)

        // Set Box Collider FALSE, no more extra points =]
        this.GetComponent<BoxCollider2D>().enabled = false;

        score.AddScore (point);

        // Destroy object after 1 sec, now can play efx sound
        Destroy (gameObject, 1f);
    }

    if (colisor.gameObject.tag == "floor") {
        Destroy (gameObject, 1.5f);

}

当前代码:

    void OnCollisionEnter2D(Collision2D colisor)
{
    if (colisor.gameObject.tag == "Bee") {

        GetComponent<AudioSource> ().Play();
        score.AddScore (point);
        Destroy (gameObject);
    }

    if (colisor.gameObject.tag == "floor") {
        Destroy (gameObject, 1.5f);

}

你不需要淡化任何东西。只需将 AudioSource 附加到未被销毁的 Bee GameObject,然后在 Start 函数中获取对它的引用。也使用 gameObject.CompareTag 代替 if gameObject.tag

AudioSource coinCollectSound;

void Start()
{
    coinCollectSound = GameObject.Find("Bee").GetComponent<AudioSource>();
}

void OnCollisionEnter2D(Collision2D colisor)
    {
        if (colisor.gameObject.CompareTag ("Bee"))
        {

            coinCollectSound.Play();

            score.AddScore (point);
            Destroy(gameObject);
        }

        if (colisor.gameObject.CompareTag("floor"))
        {
            Destroy(gameObject, 1.5f);

        }
}

编辑

你的问题中没有提到你有 3 个声音。

创建名为 COINSOUNDS 的游戏对象,然后在其下创建 3 个额外的游戏对象。将它们重命名为 COINSOUND1COINSOUND2COINSOUND3 并将 AudioSource 附加到 每个 。不要将 AudioSource 附加到 COINSOUNDS(父游戏对象)。

COINSOUND1,COINSOUND2,COINSOUND3 必须是 COINSOUNDS GameObject.

的子对象
AudioSource[] coinCollectSound;

void Start()
{    
    coinCollectSound = new AudioSource[3];
    coinCollectSound[0] = GameObject.Find("COINSOUNDS/COINSOUND1").GetComponent<AudioSource>();
    coinCollectSound[1] = GameObject.Find("COINSOUNDS/COINSOUND2").GetComponent<AudioSource>();
    coinCollectSound[2] = GameObject.Find("COINSOUNDS/COINSOUND3").GetComponent<AudioSource>();
}


void OnCollisionEnter2D(Collision2D colisor)
{
    if (colisor.gameObject.CompareTag("Bee"))
    {

        coinCollectSound[0].Play();//Play COINSOUND1
        coinCollectSound[1].Play();//Play COINSOUND2
        coinCollectSound[2].Play();//Play COINSOUND3

        score.AddScore (point);
        Destroy(gameObject);
    }

    if (colisor.gameObject.CompareTag("floor"))
    {
        Destroy(gameObject, 1.5f);

    }
}

解决方案 3

您也可以使用 coroutine。启动协程,淡入淡出图像,播放声音,等待声音播放完毕,销毁。这只能在协程中完成。这看起来比我的其他 solution.You 不必使用下面的代码修改当前场景中的任何内容。

SpriteRenderer coinSpriteRenderer;
AudioSource coinCollectSound;

void Start()
{
    coinCollectSound = gameObject.GetComponent<AudioSource>();
    coinSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
}

void OnCollisionEnter2D(Collision2D colisor)
{
    if (colisor.gameObject.CompareTag("Bee"))
    {

        score.AddScore (point);
        StartCoroutine(fadeAndWaitForSound(1));
    }

    if (colisor.gameObject.CompareTag("floor"))
    {
        StartCoroutine(fadeAndWaitForSound(1));
    }

}

IEnumerator fadeAndWaitForSound(float fadeTimeInSeconds = 1)
{

    Color currentColor = coinSpriteRenderer.color;

    Color invisibleColor = coinSpriteRenderer.color;
    invisibleColor.a = 0; //Set Alpha to 0


    float counter = 0;

    //Play sound
    coinCollectSound.Play();

    //Wait till sound is done playing
    while (coinCollectSound.isPlaying)
    {
        yield return null; 
    }

    //Now Fade texture
    while (counter < fadeTimeInSeconds)
    {
        counter += Time.deltaTime;
        coinSpriteRenderer.color = Color.Lerp(currentColor, invisibleColor, counter / fadeTimeInSeconds);
        yield return null;
    }

    //Destroy after fading
    Destroy(gameObject);
}

我会这样处理:

发生碰撞时,立即执行更新字符等操作stats/etc

将对象设置为非活动状态(如果需要)

做清理工作(播放声音等)

销毁对象

本教程中有对象被销毁但仍在播放声音。可能会有帮助:http://unity3d.com/learn/tutorials/projects/space-shooter/audio?playlist=17147