Hi, I am making a game and I have a lot of it done. When I shoot and hit an enemy they die, but some hits are not detected so the enemy remains alive. Assume the health is 30.
In update I have
if (Enemy_Health <= 0)
Destroy(gameObject);
in void OnCollisionEnter2D(Collision2D coll)I have
if (coll.gameObject.tag == "Bomb")
Enemy_Health = Enemy_Health - 30;
if (coll.gameObject.tag == "Bullet")
Enemy_Health = Enemy_Health - 30;
The bullet has a rigidbody and a box collider. While the enemies have polygon colliders(I have also tested box colliders, but not rigidbody as I have an invisible wall to prevent the player from leaving, which prevents the enemies from entering from off screen).
The code works fine but some of the collisions are not detected and thus an enemy that would die in one hit sometimes dies in 3-5hits. Could someone please help me find a solution or find any mistake in this code. Thanks.
Also in case it helps these enemies are being instantiated from another location/script(moving left) and are being destroyed if they exit the screen left. This is also being done for backgrounds(clouds, ground etc).
I also think coll.gameObject.tag == “Bomb/Bullet” might be the problem as maybe the tag is sometimes not read properly.