亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

粒子系統 OnTriggerEnter 不起作用(但所有其他功能都起作用)

粒子系統 OnTriggerEnter 不起作用(但所有其他功能都起作用)

C#
烙印99 2023-08-20 15:01:23
我有簡單的腳本,當敵人擊中玩家=負1生命時的onTriggerEnter2d等,效果完美?,F在我想向其中添加粒子系統爆炸,但什么也沒有發生。不確定我做錯了什么。玩家擁有 BoxColder2d,并勾選了觸發器。謝謝你的幫助。一些代碼:敵人: public ParticleSystem explosion; private void OnTriggerEnter2D(Collider2D enter) { if (enter.gameObject.tag.Equals("Player")) {     HartCount.HartValue -= 1;     gameObject.GetComponent<ParticleSystem>().Play();     Destroy(this.gameObject); } }我也嘗試過這個: public GameObject explosion; private void OnTriggerEnter2D(Collider2D enter) { if (enter.gameObject.tag.Equals("Player")) {     HartCount.HartValue -= 1;     Instantiate (explosion, transform.position, Quaterion.identity);     Destroy(this.gameObject); } }
查看完整描述

2 回答

?
慕俠2389804

TA貢獻1719條經驗 獲得超6個贊

考慮到您尚未將粒子系統作為游戲對象的子對象,請嘗試以下操作:


public GameObject explosion;  //drag the particle system prefab here

private void OnTriggerEnter2D(Collider2D enter)

{

   if (enter.gameObject.tag.Equals("Player")) //when the enemy collides with the Player

   {

      HartCount.HartValue -= 1;

      GameObject particle = Instantiate (explosion, this.transform.position, Quaterion.identity);

      particle.GetComponent<ParticleSystem>().Play();

      Destroy(this.gameObject);

   }

}

確保粒子系統放大到足夠大,以便它實際上可見。上面的代碼將在敵人的位置生成一個您選擇的粒子系統(您已將其拖入編輯器中的“爆炸”字段中的粒子系統)。


查看完整回答
反對 回復 2023-08-20
?
炎炎設計

TA貢獻1808條經驗 獲得超4個贊

因此,經過一番挖掘,我找到了導致您問題的可能原因。
粒子系統不會觸發 OnCollisionEnter 和 OnTriggerEnter 事件。
相反,它們會觸發一個自定義事件,即OnParticleCollision。

本質上,可以在粒子系統對象以及被擊中的對象上調用此方法。
你可以這樣使用它:

public ParticleSystem explosion;


private void OnParticleCollision(GameObject other)

{

? ? if (other.tag.Equals("Player"))

? ? {

? ? ? ? HartCount.HartValue -= 1;

? ? ? ? gameObject.GetComponent<ParticleSystem>().Play();

? ? ? ? Destroy(this.gameObject);

? ? }

}

請注意,這是您的代碼的改編副本。

它實際上還有一個問題:

你玩了粒子系統,但之后你直接銷毀了游戲對象,因此粒子系統也消失了。


注 1:

文檔缺乏有關如何檢索有關粒子碰撞的更多信息的明確信息。

鏈接頁面中的示例代碼使用如下內容:


var collisionEvents = new List<ParticleCollisionEvent>();

myParticles.GetCollisionEvents(other, collisionEvents);

其中 myParticles 是對粒子系統的引用。


但是,沒有關于此方法的文檔。

相反,有一些關于過時的靜態GetCollisionEvent 的

文檔 ,我想該文檔已經過時了,因此您應該使用非靜態方法。


注 2:

我不確定為什么敵人能夠擊中你的玩家,根據文檔,這是不應該發生的。

但也許我只是誤解了一些東西。


查看完整回答
反對 回復 2023-08-20
  • 2 回答
  • 0 關注
  • 244 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號