2 回答

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

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:
我不確定為什么敵人能夠擊中你的玩家,根據文檔,這是不應該發生的。
但也許我只是誤解了一些東西。
- 2 回答
- 0 關注
- 244 瀏覽
添加回答
舉報