Search Unity

Detecting a collision on the child of the gameobject

Discussion in 'Physics' started by Flyentology, Sep 27, 2017.

  1. Flyentology


    Aug 22, 2017
    Hi there,

    I have a game object called Character with RigidBody2D and Box Collider2D attached to it. I've created a child of that object, called Axe, and attached a circle collider to it. alt text

    Problem is that in script that handles the enemies, whenever I want to check if the collision between axe and enemy appeared nothing happens.

    Here is a code I use in EnemyController to detect collision with that child:

    Code (CSharp):
    1.  void OnCollisionEnter2D(Collision2D col)
    2.      {
    3.          if (col.gameObject.CompareTag("Axe"))
    4.          {
    5.              EnemyAnim.SetBool("PlayerCollider", true);
    6.              Instantiate(blood, rb2D.transform.position, Quaternion.identity);
    7.              Destroy(gameObject);
    8.          }
    9.      }
    Any tips how to dectect a collision between child of character and enemy?
    Thanks in advance!

  2. Hyblademin


    Oct 14, 2013
    The collision event is called inside the parent object for this configuration, since it has the Rigidbody2D. Parents of children with colliders will have collision events triggered when their children touch something.

    You can separate the detection between an object and its child by giving the child its own Rigidbody2D. Make it kinematic to preserve the motion behavior you currently have. This way, the two objects' collision events will behave the same as if they were independent objects.