【发布时间】: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 谢谢汤姆,这很好用。罗勒,我不确定我应该这样做