- Home /
Sphere collider catching edges of aligned cubes
My game allows users to create 2D levels with a tile based editor. In a level, a long platform is made up of multiple cubes. I have found that when a ball rolls over two aligned cubes (snapped precisely next to each other), the ball's collider sometimes catches an edge and bounces.
I assume there is a collision detected at some time between gravity being applied and correction of the ball's height. Is that correct and is there any way around it?
To fix this I have tried:
The DontGoThroughThings script.
Using a mesh collider on the ball instead of a sphere collider.
Playing with the project PhysicsManager settings. Decreasing Min Penetration For Penalty and increasing Solver Iteration Count.
[edit]
I got a response from support. It sounds like the suggestions made here might be the only options. Here are quotes from emails (just relevant parts).
Their response:
"You can extend the collider to make a single object, thus eliminating the gap. Otherwise you can use two Quads to make the "floor" on which the sphere will travel."
My reply:
"I just tried using quads in place of cubes and I still had the same issue. Could you confirm if this should work? Extending the collider would not work for my current game design."
[edit2]
Tested more and found there is an improvement if I use a sphere Mesh Collider and convex quads for the floor (still catches but rarely). No improvement if I use a regular sphere collider with quad floor.
Did you try making the collider just a tiny bit larger than the cube itself, so they overlap?
I'm having similar problems, in all sorts of situations, always hitting corners on flat surfaces.
@tbkn That sounds like it could work. I'll test it in the morning.
Too bad... are you sure they are overlapping? If they are, might be worth contacting unity support.
Answer by hamstar · Nov 07, 2013 at 09:26 PM
Thanks to everyone for the discussion and suggestions. Unity support got back to me with a fix. The solution was to set "Min Penetration For Penalty" to "0" in the project's physics settings. I tried reducing this value before, but wrongly assumed the value had to be greater than 0.
I can now use either box or quad colliders for the floor without problems.
Do you know why this worked for you?
I'm very new to Unity and very frustrated at the moment. I have the same problem, and this does not fix it for me. I also see other people posting the same problem, most with no answer on how to fix it. It seems like Unity 2D physics isn't very polished yet?
Based on the description of the '$$anonymous$$in Penetration For Penalty' field here : http://docs.unity3d.com/Documentation/Components/class-Physics$$anonymous$$anager.html I don't really understand how this works. It sounds to me like a higher number should fix it, but I guess I'm reading it wrong. In either case, $$anonymous$$y circle collider is still occasionally jumping off the floor when I have two box colliders right next to each other under it. Was there anythign else you did to get this working?
A higher number allows objects to overlap more without correcting their position (pushing them apart). Therefore, in my example, if I set '$$anonymous$$in Penetration For Penalty' to '0', the ball will never be inside the floor at all, it's position will be immediately corrected when they overlap. If I set a number large than 0, the ball would be allowed to overlap the floor a bit without correcting its position, and then may hit the left side of the second cube.
You can see this affect in my test project. To see this exaggerated, open Scene 2 and set $$anonymous$$in Pen for Penalty to "0.1". After playing, set the value to 0 to see fixed.
Unfortunately the setting doesn't affect 2D physics. To $$anonymous$$imise the affect you could increase the value of "Position Iterations" (make position update/correct quicker). Apart from that I don't know, you might need to ask Unity support.
As of Unity 5.2.0p1, this setting no longer exists!
http://docs.unity3d.com/ScriptReference/Physics-$$anonymous$$PenetrationForPenalty.html
Anyone know how to fix this now?
PraetorBlue the page you linked mentioned new settings regarding "Contact Offset". Have you tried setting Physics.defaultContactOffset to 0?
Answer by MattusFinchus · Oct 19, 2015 at 12:27 PM
I had exactly the same issue but with moving a cube over a series of tiled cubes. Now and then it would just stop when it hit the edge of another cube. However, after much hair-pulling, I found that in the current version of Unity (5.2.1f1) within Edit > Project Settings > Physics, changing 'Default Contact Offset' from 0.02 to 0.001 did the trick. :) I must admit to being relatively new to Unity and am not sure if this will have any other consequences, but for the game I am working on, it seems fine. :)
This totally works in Unity 5! You just ended my 2 weeks misery! Thank you so much!
I was trying all sort of things (pretty much tried everything, merging colliders, different collider types etc.) and also changing the Default Contact Offset, but never tried making it smaller than 0.01 as I felt that's already small enough and you can't set it to 0. Well, I was wrong and setting it to 0.001 or something even smaller totally helped me get rid of unwanted bumps/jumps on the edges!
THIS HELPED $$anonymous$$E QUIC$$anonymous$$LY! THAN$$anonymous$$ YOU!!!
I had to set 'Default Contact Offset' to 0.0001 to get my game to work right.
THAN$$anonymous$$S AGAIN!
It certainly doesn't help us out that to change it to a value that low, you have to type the number in; if you use the sliding feature by dragging the mouse it won't go that low.
Answer by AlienFreak · Apr 07, 2016 at 06:53 AM
Edit->Project Settings->Physics Inspector will show PhysicsManager Set 'Default Contact Offset' from 0.01 to 0.001 (I use 0.0001)
This WILL fix it as I use a ton of cubes in a 3D cubic tile based world.
This is the change that worked for me as well, I use a capsule collider over a tiled flat surface of either box colliders or mesh colliders, although 0.001 worked fine for me, and my object can travel around 800+ km/h over the surface
Answer by samifruit514 · Aug 26, 2016 at 04:47 AM
I'm having the exact same issue but the floors are made of BoxCollider2Ds. Setting the Fixed Timestep to 0.0005 instead of 0.02 seems to fix the problem, but it affects the performance overall :(
Unity team: Thanks for providing a fix for 3D, but do you have a good fix or a better workaround for 2D?
Thanks for the tip, that works for me!
I could get away with setting it to 0.005 though, so the performance hit doesnt seem to have hurt me much :)
Would be good though if the Unity $$anonymous$$m could 'fix' this for 2D, its a bit ridiculous that 2 flat colliders next to each other cause something like this. :(
Answer by Hinkel · Sep 02, 2017 at 10:04 PM
Hey, I came across the same issue in my game. With the reduction of the "Default Contact Offset" my sphere (3d) isn't bouncing upwards anymore but the edges now seem to act as walls... if I collide with one of them my sphere just bounces back as if there was an invisible wall (doesn't happen at really low speed). I tried everything from playing around with the rest of the physics settings to combining my whole scene into one single mesh via Mesh.CombineMeshes and adding a mesh collider. I'm really frustrated since this is really necessary to work for my game. Did anyone of you finally found a fully working fix for this issue? Also is there a way to get around this issue with custom physics?
Sorry for my terrible English!
Hinkel did you ever figure it out? I also have a reddit thread on this topic: https://www.reddit.com/r/Unity3D/comments/850wv2/ball_rolling_over_tiny_cracks_has_strange/
I ended up using ProBuilder mesh welding works good. Occasionally crashes on very large mesh combines/subtracts.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121226im_/https://answers.unity.com/themes/thub/images/avi.jpg)