Hello, I have an enemy with a small BoxCollider2D and a big CircleCollider2D (see the picture below please).
I use OnTriggerEnter2D to check if the player hits the small BoxCollider2D and in that case I deal damage to him.
Now I’d like to set the enemy inactive and use the big CircleCollider2D to activate the enemy when the player is in range.
The problem is the player triggers both the colliders, which means it takes damage if it hits the small collider (which is correct) and it also takes damage when it hits the bigger circle collider (which is wrong and shouldn’t happen).
Is there a way to have the two colliders behave in different ways?
I tried to put the second collider on a child and didn’t work; also I can’t use the OnCollisionEnter because my rigidbodies are kinematic.
Any idea? Thanks.
Edit:
I think I solved it, I set the circle collider as a child and added a RigidBody2D to it. If the child has a rigid body, the trigger will refer to the child and not the parent.
You could also check the Collision2D.collider type.
If its a CircleCollider2D, then do your long range stuff; while if its a BoxCollider2D you can do the close range damage.
The OnCollisionEnter2D() will be called on both gameobjects with the collision info related to the other object.
If you want the logic to reside on the enemy’s script, implement this function in one of the scripts on the enemy (or create a new script to be added to the enemy).
Doesn’t the OnCollisionEnter2D() work only if the rigid body is not set to kinematic? I think I read it somewhere. I ask because the rigid bodies of my characters are set to kinematic.
I implemented the function in the player’s script like you told me before, I think it’s better to have only the player performs this check and not every enemy in the scene. Thanks
Why not disable the other collider when it should be inactive? That way at any given time you will only have one active collider. You can also keep the state in a boolean to tell you if it’s the big or small collider that are active, and thus decide what to do in the OnTriggerEnter2D