0
\$\begingroup\$

I have my GameObject which represents a soldier in my game. It has the following colliders:

  • 1st BoxCollider2D in child named "sword". Is Trigger checked. Tag = Sword. This collider is enable when player attacks and disabled otherwise.
  • 2nd BoxCollider2D in parent GameObject. Is Trigger unchecked, since it's the body. Always enabled.

When the sword (1st) collider collides with the the body (2nd) collider, damage happens. Until now everything works fine. Here is the OnTriggerEnter2D function I have:

void OnTriggerEnter2D(Collider2D collision)
{
    if (collision is BoxCollider2D)
    {
        Debug.Log($"{this.name} collided with {collision.name}");

        if (collision == null || collision.tag == Constants.TAG_WEAPONS 
            || collision.tag == Constants.TAG_SIGHT)
        {
            return;
        }

        if (collision.GetComponent<LivingObjects>().AreEnemies(this))
        {
            Debug.Log($"{this.name} attacked with {collision.name}");

            Attack(collision.GetComponent<LivingObjects>());
        }
        else
        {
            return;
        }
    }
}

Now I want my unit to search for enemies within a said range. I added a new collider:

  • 3rd CircleCollider2D in child named "sight". Is Trigger checked. Tag = Sight. Always enabled.

Now, when the sight (3rd) collider collides with the body collider, damage is trigger into the unit colliding with the sight collider. That is because I block damage one way around with the condition if (collision is BoxCollider2D) but not the other collision happening at the same moment.

If i can discrimate collision from CircleCollider2D into BoxCollider2D, I can remove the attack function. However, I cannot see how to do it. My question is how do I know what Collider2D type is my object colliding with the other ?

I'm open to other suggestion if this is impossible.

Here is an image of the situation

\$\endgroup\$
1
\$\begingroup\$

I think you should use a layering system of unity and kept the relevant colliders in the same layer as your no. 3rd collider used for searching make a new layer name it "searching" and then player collider and enemy collider keep both in that layer("searching") and then go to Edit-> project settings -> physics2d settings that will show layer collision matrix then uncheck all other irrelevant layers I hope it will solve your problem.

\$\endgroup\$
  • \$\begingroup\$ Yeah, I'm guessing I have no choice to refactor my gameobject architecture. I tried changing the layer physics, but at the moment, every collision was handled in one script (in the parent of the go). So I guess I'll split everything with 3 objects, being the hitbox collider, weapon collider and sight collider. \$\endgroup\$ – RegularNormalDayGuy Jun 22 at 15:49
  • \$\begingroup\$ yes definitely, it will work best of luck \$\endgroup\$ – user3699039 Jun 23 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.