如何在 UNITY 中销毁已完成的行走粒子(克隆)?

时间:2021-06-24 21:12:53

标签: unity3d

VIDEO < 在视频中我有一个行走粒子,当我的玩家左右移动时会出现,但我希望它们在粒子消失后被摧毁我尝试使用 统一停止动​​作,但这只会在玩家停止移动时破坏完成粒子,之后粒子将 停止出现

这些是我复制的粒子,它们将永远留在我的游戏中并在以后造成延迟 我需要一种方法在完成后停止显示它们 enter image description here

IMAGE < 我的粒子检查员 image < 我的粒子名称 有没有办法我可以在我的代码中检查粒子是否消失或检查它是否完成然后销毁它们,因为有时销毁它会破坏所有东西并且如果我再次走路,粒子不会出现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class partscript2 : MonoBehaviour
{
    public Joystick joystick2;
    public GameObject hays2;
    public Rigidbody2D rb;
    float horizontalMove = 0f;
    // public bool show = true;
    public Animator animator3;
    // public Transform player;
    //public Transform  particleposition; 



    // Start is called before the first frame update
    void Start()
    {
        //transform.position = particleposition.position;
        animator3 = GetComponent<Animator>();
        //Destroy(gameObject, 1f);
    }
    //destroy(hays);
    // Update is called once per frame
    void Update()
    {




        if (joystick2.Horizontal <= -.2f)
        {

            Instantiate(hays2, transform.position, hays2.transform.rotation);



        }

        if (joystick2.Horizontal >= .2f)
        {

            Instantiate(hays2, transform.position, hays2.transform.rotation);




        }



    }
}


1 个答案:

答案 0 :(得分:1)

首先,考虑到如果您的游戏以每秒 60 帧的速度运行,您将最终每秒创建 60 个游戏对象,这可能并且很可能会影响性能,尤其是在您正在使用的移动设备。

但进入正题,你可以调用Destroy方法并传递粒子的持续时间。

GameObject hay = Instantiate(hays2, transform.position, hays2.transform.rotation);
ParticleSystem p = hay.GetComponent<ParticleSystem>();
float duration = p.duration + p.startLifetime;
Destroy(hay , duration);

如果您的粒子的持续时间是固定的,您可以随时跳过我们通过脚本获取持续时间的部分,只需将其传递给 Destroy 方法即可。

GameObject hay = Instantiate(hays2, transform.position, hays2.transform.rotation);
Destroy(hay , 5f);