Saturday, 24 May 2008

Collision Detection

I had a play with the physics engine in Unity, in particular the capsule collider. I thought that in order to make my asteroids game a bit interesting I would make the asteroids bounce off each other when they collide.

I was avoiding doing the collision detection in Unity at first because I thought it might be quite involved with loads of Vector Math... When I got around to doing it I was, one: very surprised at how easy it was and, two: extremely impressed with the implementation of collisions in Unity.

This is what I did:

1) I created a physic material, called it Rock (The name isn't important) and had a play about with the settings. My settings were: 0.8 for Dynamic Friction, Static Friction and Bouncyness. Average for Friction Combine and Bounce Combine.

2) In my asteroid prefab I added a capsule collider and set the Material to the Rock physic material I just created. I had to set the capsule collider radius, height and direction to match the shape and size of my asteroid mesh.

3) Sat amazed at how easy and effective this was in Unity!!!


  1. Hi there! Nice improvements! Your asteroid game is nice.

    Here is my suggestion for step two:

    Alternatively, you can use a mesh collider that will take the form of the mesh of your asteroids so that you don't have to mess with the size of the capsule colliders.


  2. Hi

    I'm not actually making any more changes to the game but on this point:

    I don't actually have to mess around with the size of the capsule for the collider as it is set only once in the prefab. As each meteor is instantiated and resized the capsule is automatically resized along with the rest of the meteor!

    When the meteor rotates, so does the capsule as it is a child of the game object.

    Another great set of features in Unity!

  3. I am aware of what you're saying but that is because you have only one shape/asset of asteroid. What if you had more than one shape? You would have to set the capsule for every different asteroid asset in your prefabs.

    The mesh colliders are good if you are using different asteroid models. In that case, mesh colliders would be a better choice because they save you time. They are also more precise so if you're ever using more complicated models, they are the way to go.

  4. OK, there's a few issues with this.

    1) Mesh colliders don't interact with each other by default. My Asteroids bounce off each other so this is no good. To enable mesh colliders to 'collide' with each other they must be marked as Convex.

    2) Convex colliders must have fewer than 255 triangles - my meteors don't, they have nearer to 700!!!

    3) Although a mesh collider is more accurate as you correctly suggest there is more mathematical overhead. And as the meteors are of general capsule shape I think this suits this particular implementation.

    The Unity documentation suggests that mesh colliders are 'most useful for background objects' for which I tend to agree.

    You could of course create a more basic mesh which follows the general shape of the meteor but that's more work! And it only takes 10 second to set the capsule collider up.

  5. Sorry, I didn't know they had that many triangles.

  6. i having some troubles in collision, my player bounces a bit but thats no matter, the problem is when i keep pressing forward yet colliding the player shakes... is how if it tries by all ways to get thought the wall... what could be?


  7. Hardy...

    Not sure I fully understand your problem. Have you tried tweaking the Physics Manager settings?


  8. hi JT, well, tweaking that i´m not tested yet, could you tell me where to find that?, are those settings in Edit, Project Setting, Physics? , or some other place where to locate them?.. Thank you

  9. That's where to find it yeah. I'm no physics expert, but I know you can get jittery objects if they aren't right. I've had wobbly dice at rest when I played around with them. I found it best to leave them alone.

    If your player is bouncing it may be the physic material you are assigning to the collider. You can change the Bouncyness setting in the project asset, and maybe have a tweak of the friction settings.

    I would create a new physic material rather than tweak the standard assets.

  10. ok !, understood thank you !