2016-04-27 32 views
2

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

我无法使用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); 

} 

回答

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个声音没有提到。

创建GameObject命名为COINSOUNDS然后在其下创建 3个其他GameObject。将它们重命名为COINSOUND1,COINSOUND2,COINSOUND3并附加AudioSource各自其中。不要将AudioSource附加到COINSOUNDS(父GameObject)上。

COINSOUND1COINSOUND2COINSOUND3必须是COINSOUNDS游戏对象的孩子。

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

工作正常呵呵,但我有3个不同的硬币,因为我与其他2吗? –

+0

@AlanVieiraRezende附上相同的脚本到你的3个不同的硬币。它仍然会做同样的事情。播放声音然后被摧毁 – Programmer

+0

我表达不好,3种不同的硬币sfx的声音。 –

0

我想接近它是这样的:

冲突,做即时之类的东西更新字符统计的/ etc

设置对象为不活动(如果需要)

待办事项清理东西(播放声音等)

毁坏对象

本教程的对象被破坏,但仍然在播放声音。这可能是有帮助的:http://unity3d.com/learn/tutorials/projects/space-shooter/audio?playlist=17147

+0

我尝试设置Inactve,但我不希望被冻结在屏幕上的对象被摧毁,我也尝试setKinemact False,但没有办法令人满意。 –

+0

所以你希望它仍然可见,只是不动?我不确定我了解你想要达到的目标。 SetActive = false不会将其销毁,只是将其禁用,您可以通过执行SetActive = true将其重新打开 –