首页 > 解决方案 > 销毁命令不适用于粒子系统

问题描述

当我用我的枪射击(发送 TakeDamage 功能)时,枪管爆炸首先它变得不可见,然后在 2.1 秒后被摧毁。但本应在 2s 内销毁的粒子 System 继续循环。

这是代码;

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

public class Target : MonoBehaviour
{
    public float health = 50f;
    public ParticleSystem explode;

    public void TakeDamage(float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }
    }

    public void Die()
    {
        ParticleSystem PartClone = Instantiate(explode, gameObject.transform.position, gameObject.transform.rotation);
        PartClone.Play();


        gameObject.SetActive(false);
        ParticleSystem.Destroy(PartClone, 2f);
        Destroy(gameObject, 2.1f);

    }


}

标签: c#unity3d

解决方案


将您ParticleSystem explode的转换为包含此粒子效果的游戏对象(预制件)。

用 实例化它GameObject PartClone = Instantiate(...)

用 销毁它Destroy(PartClone, 2f)


完整的代码应该是这样的。在您的粒子预制件中,将 ParticleSystem PlayOnAwake 属性设置为 true,因此您无需Play()在实例化后调用。

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

public class Target : MonoBehaviour
{
    public float health = 50f;
    public GameObject explode;

    public void TakeDamage(float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }
    }

    public void Die()
    {
        GameObject PartClone = Instantiate(explode, gameObject.transform.position, gameObject.transform.rotation);    

        gameObject.SetActive(false);
        Destroy(PartClone, 2f);
        Destroy(gameObject, 2.1f);    
    }   
}

推荐阅读