Hi, I’m working on action game and I have a problem with guns because they can shoot through walls, check this gif:
The player has a character controller, the gun is a child to player’s hand. The gun has a box collider (not trigger) with a kinematic rigidbody and doesn’t use gravity. But as you can see it doesn’t collide with anything at all and allows player to kill enemies through walls which is obviously not very good.
I’ve tried a gun with a collider only but that doesn’t work too.
Of course I could’ve increased character controller radius so that it includes the whole gun but that makes impossible to the player to walk through thin corridors.
So, any ideas why doesn’t this setup work and how can I solve this problem?
Okay, I’ve managed to solve that issue. Like @Cherno said, child colliders can only affect rigidbody-based character but switching from character controller to rigidbody was not an option in my case.
So, now my setup looks like this: every gun has a trigger collider. a kinematic rigidbody and a collision checking script. That script simply has a bool variable canShoot, which is set to be true in every FixedUpdate and is set to be false OnCollisionStay (so now we can know whether it collides with anything at any given frame). And then we use that bool to allow or block every single shot, simple!
Check that gif:
Go onto the character controller, remove the collider and change it into a box collider. From there, edit the collider, to make it fit the character and the gun. That should work.
If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.