So my player and enemies are setup just like in the image attached. My problem is that when the player’s attack trigger enters the enemy it triggers the line of code for taking damage twice as there are two box colliders attached to the enemy gameObject.
This is my code for checking if the player’s attack trigger enters the enemy box collider:
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "AttackTrigger")
{
Debug.Log("Enemy take damage.");
TakeDamage();
}
}
So yeah because I have two box colliders attached, and the script is checking for collisions to any box collider, it runs the TakeDamage() function twice when the player’s attack trigger enters either collider.
I’m sure I’m being an idiot and I’m fairly new to coding but could anyone help me out with a solution?
Another way and it must work…which is using unity layers… to stop uneeded interactions between colliders.
add your second collider ( the one you want to be ignored by attack collider) to a new layer.
then go to your physics settings, on the matrix… uncheck it’s interaction with collider layer that is assigned to attack trigger collider.
then it wouldn’t detect, also this improves performance if you have many colliders in your scene that you never want your attack colliders interact with.
I did however just find a solution that seems to be working fine Instead I attached a collider to the player that detects any enemies that enter and it seems to work as expected. Thank you for your help. <3