- Home /
Collider attached to a character controller doesn't work
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?
$$anonymous$$aybe try makign the Guns collider non-kenematic?
The gun just floats in space if it's rigidbody is non-kinematic.
You'd nbeed a RB-based character with a compound collider that includes a collider for the weapon for this to work the way you want it.
While this solution indeed makes it impossible to the gun to go throught walls, it also adds more complexety and problems to my project so I'd rather stick to using just a character controller.
Hmmm, I'm not sure there's any other way to do it other than what @Cherno said (and I'd rather have a solution with a lot of work than no solution at all).
Answer by GlitchCrew · Oct 12, 2017 at 03:38 AM
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:
Answer by connorwforman · Sep 05, 2017 at 10:45 PM
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.
I can't remove collider from the character controller because there is none attached. Anyway, I've tried ADDING box collider to the player and it didn't change anything at all. The collider just doesn't collide with anything.
Answer by unit_nick · Sep 06, 2017 at 05:12 AM
If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html
Your answer
Follow this Question
Related Questions
Guidelines for using rigidbody, collider, CharacterControllerScript, etc? 3 Answers
How to deal the 2d platform physics? 0 Answers
Can't raycast on Character Controller? 1 Answer
Static collider vs Character controller? 1 Answer
CharacterController.Move becomes really slow after I move seperate colliders 1 Answer