- Home /
box Colliders
Hello, i added two colliders to my 2D player. but when the player touch an enemy it takes 2 hearts from his life. that because the enemy is colliding with two different colliders. how can i solve this problem?
here is the code : if (gameObject.CompareTag("Enemy") && playerHealth > 0) { if (doubleJump > 0 && rb.velocity.y < 0) { kills++; Destroy(gameObject); return; } else { playerHealth--; return; } }
Answer by Ercova · Aug 06, 2021 at 06:53 PM
2 different things come up to me First You may seperate your collider with a child object and add a collider and tag two different collider differently and maybe use that Code; (I assume you put this script on the player)
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Enemy") && playerHealth > 0)
{
if (doubleJump > 0 && rb.velocity.y < 0)
{
kills++;
other.gameObject.GetComponent<Collider>().enabled = false;
Destroy(other.gameObject);
}
else if (gameObject.CompareTag("Tag you want to collide with enemy"))
{
playerHealth--;
}
}
It doesnt work as u said. maybde i didnt understand u well. but my code is on the player. i mean my ontriggerEnter method is on the player. So i cant tell it that if the player collide with his self so the playerHealth--;.
what i mean is, if the script is on the player. So the method is going to detecte if the player is colliding withg another collider like an enemy.
I hope u understand what i mean and sorry if i miss understood you. thank you for your replaying!
Answer by strikenowhere · Aug 06, 2021 at 07:32 PM
Hello @Unchartedx25,
It may be worth looking into using a CompositeCollider2D instead of worrying about two separate colliders both triggering collisions as use could use the single CompositeCollider2D object in your logic instead. You can keep your original colliders in place, just set their "UsedByComposite" property to true and they should automatically get incorporated into the composite collider.
Your answer
![](https://koobas.hobune.stream/wayback/20220613041314im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 1 Answer
No Collision in 2d Scene 0 Answers
Rigidbody will overwrite children colliders, and collision events won't run. -5 Answers
Will an empty gameObject block my trigger? 1 Answer
Enemy gets through wall over time, colliders working. 1 Answer