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)
  • 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.

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.