只实例化一次产卵

本文关键字:一次 实例化 | 更新日期: 2023-09-27 18:24:16

我做了一个灰尘效果,它可以工作,但只工作一次,这是我的代码:

if ((Input.GetKey (KeyCode.UpArrow) && (grounded == false))){
            if (ativar < maxAtivar){
                Particula ();
                Destroy (GameObject.Find ("Poeira(Clone)"), delay);
            } 
        } 

粒子函数:

void Particula(){
    Instantiate (prefab, new Vector3 (player.transform.position.x, player.transform.position.y, 0), player.transform.rotation);
    ativar++;
}

我不知道如何重复,建议如下:D

只实例化一次产卵

您需要将代码放在UpdateFixedUpdate方法中,如下所示:

void Update()
{
    ...
}
void FixedUpdate()
{
    ...
}

不过,了解两者之间的区别很重要。FixedUpdate每帧运行一次,与物理引擎同步,而Update则不运行。

 Destroy (GameObject.Find ("Poeira(Clone)"), delay); **this destroys every dust object in your scene so;**
if ((Input.GetKey (KeyCode.UpArrow) && (grounded == false))){
            if (ativar < maxAtivar){
                Particula ();
               //close this Destroy (GameObject.Find ("Poeira(Clone)"), delay); 
            } 
        } 
    void Particula(){
       GameObject obj =(GameObject) Instantiate (prefab, new Vector3 (player.transform.position.x, player.transform.position.y, 0), 
      player.transform.rotation);
       Destroy (obj, delay); //destroy here 
        ativar++;
    }