Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[solved] 3D child collider seemingly messing with parent C# collision detection

Discussion in 'Scripting' started by ralf_b, Sep 7, 2017.

  1. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Hello everybody,

    unfortunately I am a bit confused about what really is happening, but I suppose a child object (sword) which also has a box collider sends it´s collision detection "up" to the parent (Player) so that when I hit the enemy with the sword both detect a hit and loose a life. It looks like collision detection is
    Is there maybe a way to exclude the detection of children? Or at least detect which child object collides with which other object?

    My setup

    Player (Rigidbody, C#, Box Collider)
    +-- Sword (Box Collider)

    Enemy (Rigidbody, Box Collider)


    I found a similar post but it is about 2D which maybe a bit different to cope with?
    https://forum.unity3d.com/threads/h...e-parent-collider-script.485274/#post-3160734
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Show us your code for your collision. You can check what you are being hit with or what you are hitting based on the parameter that is passed in.
     
  3. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    I do check for collision from both sides.

    Player -> Enemy (a C# script on the player checks if player collides with enemy player loses life)
    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.             if (other.gameObject.CompareTag("enemy")) {
    3.              print("PLAYER HIT");
    4.             }
    5.         }
    Enemy -> sword (a C# script on the enemy checks if enemy collides with sword enemy loses life)
    Code (CSharp):
    1.     void OnTriggerEnter(Collider other) {
    2.         if (other.gameObject.CompareTag("sword")) {
    3.            print("ENEMY HIT");
    4.         }
    5.     }
    Now comes the problem:
    The player registers a hit in both situations ...
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    So your player has a box collider with a script to compare sword and then the player takes damage?

    Or does the sword check for enemy and then the sword does damage?
     
  5. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    Nope.
    Player has a script and a collider box which check if it collides with enemy.
    +-- In / below the player there is a sword with a collider box.

    The enemy has a script and a collider box and checks if it collides with the sword.

    Problem is:
    When the sword hits the enemy both the enemy and the player register a hit.
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Ok, sorry I think I mistyped.

    I understand now. The player can hit the enemy, thus losing a life. Or the sword hits the enemy, thus killing the enemy.

    Try adding a kinematic rigidbody to your sword.
     
    ralf_b likes this.
  7. ralf_b

    ralf_b

    Joined:
    Jul 9, 2013
    Posts:
    48
    HOLY SMOKES! Thanks Brathmann, all I needed to do was add the rigid body and now everything works as it should :D

    I have read several times that collisions are passed upwards the hierachy to the top most rigid body, so I did not expect a rigid body on a child would stop that.