- Home /
OnCollisionEnter2D not triggering on parent
Hello!
I'm having trouble with my project, where OnCollisionEnter2D is not triggering at all.
This is the object: test
"ragdoll" settings:
The "head" has a Circle Collider 2D with a Dynamic Rigidbody and a Hinge Joint 2D attached to the torso.
The platform that the "head" collides with has a Box Collider 2D and a Rigidbody 2D that is static.
All colliers have "Is Trigger" disabled.
The Code:
void OnCollisionEnter2D(Collision2D collision)
{
locked = false;
}
The code never reaches "locked = false;".
I tried adding the following code to the children:
public class ChildCollider : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D collision)
{
GetComponentInParent<Collider2D>().attachedRigidbody.SendMessage("OnCollisionEnter2D", collision);
}
}
While this triggers correctly on "head", it only causes a stack overflow in Unity that is only resolved with a restart of the program.
I am using Unity 2017.1.1f1, the latest.
Similar questions: https://answers.unity.com/questions/1346745/oncollisionenter2d-is-not-being-called.html
https://answers.unity.com/questions/1408973/oncollisionenter2d-is-not-working-2.html
https://answers.unity.com/questions/1396306/c-oncollisionenter2d-failing-to-respond.html
https://answers.unity.com/questions/1386796/ontriggerenter2d-and-oncollisionenter2d-not-workin.html
Answer by BreakTheWInd · Jan 14, 2018 at 02:12 PM
Okay, I have solved it by adding the following code to the Children:
void OnCollisionEnter2D(Collision2D collision)
{
transform.parent.gameObject.SendMessage("OnCollisionEnter2D", collision);
}
From what I know, the parent should detect collision when it happens in children. Also, the parent is fixed to the Torso with a Fixed Joint, and everything else is connected to the torso with hinges.