【问题标题】:Destroy particle system when initalised programmatically以编程方式初始化时破坏粒子系统
【发布时间】:2018-05-29 09:04:02
【问题描述】:

我有一个玩家和一个敌人的游戏。

我还有一个粒子系统,当玩家死亡时,比如爆炸。 我已将此粒子系统制作为预制件,因此我可以在每个关卡中多次使用它,因为有人可能会死很多次。

所以在我的enemy.cs 脚本中,附加到我的敌人,我有:

public GameObject deathParticle;

private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.name == "Player" && !player.dead){
            player.dead = true;
            Instantiate(deathParticle, player.transform.position, player.transform.rotation);
            player.animator.SetTrigger("Death");
        }
    }

所以当玩家被敌人杀死时,这会播放我的粒子系统。现在在我的播放器脚本上,我有这个。这个特定的功能会在死亡动画之后播放:

public void RespawnPlayer()
{
    Rigidbody2D playerBody = GetComponent<Rigidbody2D>();
    playerBody.transform.position = spawnLocation.transform.position;
    dead = false;
    animator.Play("Idle");

    Enemy enemy = FindObjectOfType<Enemy>();
    Destroy(enemy.deathParticle);
}

这会像往常一样重生玩家,但在我的项目中,每次我死时我都有一个我不想要的死亡(克隆)对象。最后两行是为了删除它,但它没有。

我也试过这个没用:

Enemy enemy = FindObjectOfType<Enemy>();
ParticleSystem deathParticles = enemy.GetComponent<ParticleSystem>();
Destroy(deathParticles);

【问题讨论】:

  • 玩家死亡时不破坏敌方粒子,一定时间后能不破坏原来的粒子吗?:GameObject newParticle = Instantiate(deathParticle, player.transform.position, player.transform.rotation) as GameObject; Destroy(newParticle, 5f); // Destroy the particle after 5 seconds
  • 如果敌人对象中的死亡动画是为玩家设计的,为什么它会存在?执行FindObjectOfType 很昂贵,如果玩家已经拥有预制件,则没有必要。
  • @Tom 谢谢汤姆,这很好用。罗勒,我不确定我应该这样做

标签: c# unity3d


【解决方案1】:

没有必要InstantiateDestroy死亡粒子,这会产生很多开销,你可以简单地在你想要它启动和停止它时重播它,当你不需要它时

 ParticleSystem deathParticleSystem;

    private void OnTriggerEnter2D(Collider2D collision)
        {
            #rest of the code
            deathParticleSystem.time = 0;
            deathParticleSystem.Play();

        }
    public void RespawnPlayer()
    {
        //rest of the code
        deathParticleSystem.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
    }

或者您可以enabledisable 与您的粒子预制件关联的gameObject

  public GameObject deathParticlePrefab;

        private void OnTriggerEnter2D(Collider2D collision)
            {
                #rest of the code
                deathParticlePrefab.SetActive(true);

            }
        public void RespawnPlayer()
        {
            //rest of the code
            deathParticlePrefab.SetActive(false);
        }

【讨论】:

    【解决方案2】:

    您总是可以创建一个新脚本并将其分配给在一定时间后销毁它的预制件:

    using UnityEngine;
    using System.Collections;
    
    public class destroyOverTime : MonoBehaviour {
    
       public float lifeTime;
    
       // Use this for initialization
       void Start () {
    
       }
    
       // Update is called once per frame
       void Update () {
           lifeTime -= Time.deltaTime;
    
           if(lifeTime <= 0f)
           {
               Destroy(gameObject);
           }
       }
    }
    

    因此,在这种情况下,您可以将其分配给您的 deathParticle。如果您要实例化对象,则此脚本在多种情况下都很有用,这样您就不会加载不必要的对象。

    【讨论】:

    • 谢谢。我认为如果我要制作一个新脚本,一种更简单的方法就是做particle.isPlaying,但最后我只是在播放器脚本中添加了一个0.7秒的计时器
    • @RachelDockter 我认为这会在动画播放之前破坏粒子
    • @RachelDockter 抱歉,我想我可能不清楚这个问题,是粒子继续播放并且即使在玩家死亡后仍然出现在屏幕上的问题,或者仅仅是有负载层次结构中的克隆对象数
    • 它不循环,视觉上它工作正常,但它只是制作了很多克隆对象。但你的方式解决了这个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多