Colliders and Rigidbody

Hi, I have a ‘bullet’ object which has a capsule collider. As a test to ensure I was understanding Unitys collision code, I created a second object - a cube with a box collider.

When the two collide, no collision was being reported (using OnCollisionEnter). I then noticed in the docs it advised attaching a rigidbody to objects which move a lot, so I attached one to the bullet (and made it Kinematic). No collision. So I then attached a rigidbody to the cube which is static and it reported the collision. Hmm. (Also when I made the cube kinematic it stopped reporting the collision!?)

So essentially, I have two objects with rigidbodies, one kinematic, one not just so the collision will work - aside from being confused about whether something is going wrong, this seems overkill to me and sounds expensive given I want lots of bullets and cubes (ie monsters) on the screen.

All I really want is to report a collision so I destroy both objects and replace them with an explosion - nothing too clever!

Am I missing something?
many thanks

How fast does your bullet object move? I think if its going fast enough it could be passing through the box collider (one frame its on one side then the next its on the other side). If that’s the case you might try resizing your box collider or playing around with the collision detection settings on the rigidbody. Also, if your bullet objects are moving too fast to really see, you might want to look into raycasts. I haven’t used them yet but I hear they are good for your typical bullets.

If you want to get by with fewer rigid bodies, consider OnTrigger() or OnParticleCollision(). The pre-conditions for these often require fewer rigid bodies to satisfy than the requirements for OnCollision. The Unity doc page I find most helpful for debugging this sort of thing is:

Waaaaay down at the very bottom there’s a table. :slight_smile:

Thanks for the response guys - Bauer, bullet is moving really slow, I halved it’s speed and doubled the thickness of the cube, but this didnt make any difference - with these changes it’s ‘in’ the box collider for about a second now, so it can’t be the speed.

Ostagar - I’ll look into these alternative solutions, but from the link it seems that the bullet should have a rigidbody attached (as it moves) but the cube (as it’s static) doesn’t need to…curiouser and curiouser!

Yeah. Your bullet was a kinematic rigidbody, and a kinematic rigidbody + static generate trigger events but not collision events. If you made your bullet a non-kinematic rigidbody then it would generate collisions whenever it came into contact with the static object. It’s late, but I think I said that right. :wink:

Ostagar, that’s fixed it (was that in the documentation and I missed it??) - thankyou so much.

Glad to help. It was in that table I pointed you to. Granted that table, while chock full of helpful info, is a bit tricky to read the first time you see it due to the unusual table formatting.

I’m not trying to be nitpicky, but why does the bullet need a capsule collider? I think a sphere would be sufficient and slightly more efficient.

A cube would be even simpler, and good enough.

Not using a rigidbody at all would be even more efficient, especially if these bullets are just going to be moving in a straight line. I’d use physics for something like a cannonball, but not for bullets. Just do a raycast (delayed, if you want to simulate bullet travel time), and if you really do want each bullet to be a visible entity and it would actually be moving slow enough to see it, just spawn a really simple mesh with a script attached to make it move in a straight line until it arrives at the location that the raycast hit.