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

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?

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.

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)

void OnTriggerEnter(Collider other) {
            if (other.gameObject.CompareTag("enemy")) {
             print("PLAYER HIT");
            }
        }

Enemy → sword (a C# script on the enemy checks if enemy collides with sword enemy loses life)

    void OnTriggerEnter(Collider other) {
        if (other.gameObject.CompareTag("sword")) {
           print("ENEMY HIT");
        }
    }

Now comes the problem:
The player registers a hit in both situations …

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?

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.

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.

1 Like

HOLY SMOKES! Thanks Brathmann, all I needed to do was add the rigid body and now everything works as it should :smile:

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.