- Home /
Box collider won't collide as a child?
I've got a player, who carries a weapon (both of which are just 1x1x1 cubes at the moment). The weapon is a child of the player. Both the weapon and the player have a box collider.
I have another cube which is my enemy. The enemy has a box collider which is a trigger. The enemy's script logs a notification on any intersection with the weapon.
Or it should.
For some reason, my weapon won't collide with anything. It goes through walls, enemies, etc.
I have tried making my weapon independent - no longer a child of the player, but a sister to it - and by giving it a character controller and adding my player script to it, I can run my weapon around the field and it will collide with anything, and also will log intersections with the enemy.
Seeing that this worked, I also tried adding a character controller to my weapon once I had restored it to it's rightful place as a child of the player. It didn't help.
So my question is this: what is it about being a child or not having a script that causes my weapon's box collider to lose it's all-important collision ability?
UPDATE: I've added a rigidbody to the weapon. When "is kinematic" is checked off, the weapon behaves in unexpected ways. When "is kinematic" is on, I get the trigger detection I was looking for, but the weapon still passes through walls (which I would prefer not to have). And why should I need a rigid body to make a collider work? It seems like simple collisions and complex physics are two distinct things...
all i can help with is that every time you want to do the OnTriggerEnter you need to have two colliders with at least one of them having a rigidbody and one has the trigger box checked
did you make sure your weapon didn't have the trigger option checked on it ?
Thanks for the input. As you see from my update, I added a rigidbody which enabled my trigger (which is on the enemy, not the weapon) to work. The only issue now is that the box collider doesn't collide with walls and such. While this is a problem, it's not a big problem, so I've pretty much just decided to move on with development without worrying about it, but this is still a curiosity...
ah yes , to solve this kind of problem you usually put on the same object two colliders one big collider that is the trigger and one smaller collider that make the enemy collide into walls
Hi @JustinTheSwift! The official definition of the function "OnCollisionEnter(...)" says:
Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.
So when you want to use collision events (Unity's physics), it is always required that one of the objects must have a rigidbody.
"but the weapon still passes through walls (which I would prefer not to have)" If you would like the weapon to have a rigid body, it should NOT be a child of the player. Ins$$anonymous$$d, connected it with a FixedJoint (or SO$$anonymous$$E kind of joint.) This will connect the two rigid bodies, using physics. So, when say, moving forward with a spear poking out in front; when the weapon hits the wall, the impact force will be felt by BOTH the weapon AND the player (AND any OTHER objects connected by joints.)