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.