- Home /
Box collider doesn't work when same size as mesh.
The general Effect I'm creating here is a cube that splits into smaller cubes when destroyed that can then be picked up by being run into by the character controller, with each one adding to the 'score'. The sequence I'm using is this:
Main block (1x1x1) destroyed. - Prefab of a 2x2x2 arrangement of 8 block 'fragments' (0.5x0.5x0.5 cubes) instantiated. - Smaller cubes are given a random Vector3 force when created so they 'explode' out.
The way the prefab of the smaller cube arrangement is done is using an empty as a parent which is the GameObject that is instantiated:
BlockFragments (Empty - just the parent for instantiation) - BlockFragment 1 (Cube) - BlockFragmentCollider (Empty) - BlockFragment 2 (Cube) - BlockFragmentCollider (Empty)
And so on with 8 fragments.
The parent has a simple script to remove it if all the children have been removed (picked up).
The fragments have a script giving them a random Vector3 force, and removing them when their child is removed.
The collider empty has a script that removes it when it collides with the player (OnTriggerEnter) and some code to add to the 'score'.
Each BlockFragment cube has a box collider and a rigidbody. The BlockFragmentCollider then has its own box collider and rigidbody, with Is Trigger and Is Kinematic enabled as enabling Is Trigger on the actual cube makes it fall through the floor.
The problem I'm having is that, when the box collider on the collider object is the same size as its parent mesh, it doesn't activate the OnTriggerEnter whatever I do when it collides with the character controller. However, when the collider is 2x the size of the mesh, the whole thing works fine and as planned.
Does anyone have any idea why this is happening, and what I could do to get it working with a collider the same size as the actual cube fragment mesh as having it larger throws up other problems and causes issues with raycasting.
Thanks in advance for any assistance.
So each $$anonymous$$i-block has a collider and a trigger, both the exact same size? Seems overly complicated. Is there a reason not to have just a collider?
I played around for a while trying to get it to work like that, but when I set the $$anonymous$$i-block's collider to Is Trigger, it just made the blocks fall through the floor. The problem is that I can't enable Is $$anonymous$$inematic on the rigidbody to stop this as I need the forces to be applied so it 'explodes' out - enabling Is $$anonymous$$inematic results in it not moving at all from what I can see - or at least, not being affected by forces.
But, do you really need a trigger at all? OnCollisionEnter
will usually check for a hit just as well as OnTriggerEnter
.
Ah, I didnt want to use collisionenter as I'm using a Character Controller so have to use OnControllerColliderHit and do it controller-side, but I guess it saves a lot of unneeded extra colliders and rigidbodies + scripts and I just got it working fine that way. Thanks for the help, I appreciate it!
Answer by aldonaletto · Jun 08, 2013 at 04:11 PM
If the small blocks are pickup items, you really should use triggers bigger than their colliders. The CharacterController must penetrate a trigger a little (not so little) in order to generate an OnTriggerEnter event: if the trigger isn't big enough, the character will be stopped at the internal collider and no trigger events will occur.
In order to avoid raycast problems, you can clear the checkbox Raycast Hit Triggers in the Physics Manager. Another possibility is to place the small blocks in a specific layer and do the raycasts with a suitable mask - if the cubes are in layer 8, for instance, use the negated mask ~1<<8 to check all layers but layer 8.
Anyway, if you want to use OnCollision events, be aware that they only are reported when the rigidbody hits the CharacterController, not the other way around. On the other hand, OnControllerColliderHit does the opposite: it's reported only when the character hits a collider. OnCollisionEnter may be used when the blocks are moving, but will fail if they are at rest.
Your answer
Follow this Question
Related Questions
100 OnTriggerEnter Or Tags each player too much? 3 Answers
OnTriggerEnter works, OnTriggerExit doesn't 3 Answers
OnTriggerEnter/Exit called unexpectedly 2 Answers
How to use OnTriggerEnter? (JS) 1 Answer