c# - 销毁命令不适用于粒子系统
问题描述
当我用我的枪射击(发送 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);
}
}
解决方案
将您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);
}
}
推荐阅读
- c++ - 为什么 ++xa 左值和 x++ 是右值?
- vb.net - Async Await Task.Run 更新模态表单抛出异常
- javascript - window.XMLHttpRequest.prototype.send 仅适用于控制台
- typescript - tsconfig“lib”选项的用例是什么?
- user-interface - 用例到命令/应用层映射:实现
- dji-sdk - Matrice 210 上的 FPV 摄像机没有视频输入
- nix - 可重现的 nix-env -i 只有 Nix,没有 NixOS
- node.js - 使用 HTTPS 时是否需要额外加密?
- c++ - 为什么这个针对给定挑战的解决方案不起作用?
- wso2 - 使用配置文件添加 WSO2 IS Oauth / OpenId Connect Service Provider