淡出物体

时间:2016-04-27 06:14:17

标签: c# unity3d unityscript

如何在碰撞后立即淡出一个物体?

我无法使用Destroy (gameObject);,因为该对象是一个带有efx声音的硬币游戏,如果我在玩家与硬币发生碰撞时没有播放对象声音就会破坏它。

如果我不摧毁它,立即投币,每次碰到这枚硬币就会获得积分

我正在使用音频混音器,所以真的需要来自音频源的硬币声音才能在我的设置上设置音量。

我的想法:

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);

}

2 个答案:

答案 0 :(得分:1)

你不需要褪色。只需将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 的GameObjects,然后在其下创建 3 额外的GameObjects。将它们重命名为COINSOUND1COINSOUND2COINSOUND3并将AudioSource附加到每个。不要将AudioSource附加到COINSOUNDS(父级GameOBject)。

COINSOUND1COINSOUND2COINSOUND3必须是 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。启动协同程序,淡化图像,播放声音,等待声音完成播放,然后销毁。这只能在协同程序中完成。这看起来比我的其他解决方案更好。您不必使用下面的代码修改当前场景中的任何内容。

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);
}

答案 1 :(得分:0)

我会这样做:

在碰撞时,立即执行更新角色统计/等等

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

做清理事情(播放声音等)

销毁对象

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