Question by
Leslieghf · Feb 19 at 05:04 PM ·
oncollision
OnTriggerEnter2D triggers same code multiple times concurrently, should only do so sequentially
The basic setup:
A bullet with a trigger collider, which upon hitting an enemy, starts some code to check for piercing, inflicting damage, etc.
But when there is a heap of enemies, OnTriggerEnter2D is, for obvious reasons, being called multiple times in the same frame, which I haven't even been able to circumvent with a bool isCollidingWithEnemy.
Anyone got a suggestion?
Thanks in advance, Leslie
Comment
Answer by Monsoonexe · Feb 21 at 12:15 AM
You could add a bool hasCollided = false;
property to each bullet. The first time it collides, it sets this property to true
. If this property is already true
, then exit with return
and don't execute the rest of the code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613053232im_/https://answers.unity.com/themes/thub/images/avi.jpg)