- Home /
Unrealistic gap between two objects.
Look at this picture, and please tell me why the upper square wont fall down to the "ground" underneath it. Both are box colliders2D, with the size 1, 1. And they have a rigidbody2D. The upper one have a gravity scale of 5 and a mass of 99, the lower one has is kinematic. Other than that, the rigidbody2d is just standard for both of them.
Have you tried creating a joint between the two objects?
Smoggach, no. Which one should I use? And this thing happens to ALL my gameobjects that are supposed to fall to this floor.
Try any of them just to see if it will stick them together. Your problem might also have to do with the scale of your world. The object may only think it's resting on the ground because it can't move any lower without going through it. Look up physics scale problems and i'm sure you'll find your answer somewhere.
I now tried with the distance joint. But you see, I need to push the box to the hole to the right. Then the box starts acting all weird when I run into it. Gap is gone between those two now though. But don't think this is the solution to my problem.
Answer by kacyesp · Aug 31, 2014 at 04:28 AM
I googled around and apparently someone found a work around that worked for them: http://answers.unity3d.com/questions/609792/2d-collider-arent-exact.html
Apparently if you use edge colliders instead of box colliders, your problem will go away.
Okay, it seems to help with the gap. But now I just need to figure out why my void OnCollisionEnter2D(Collision2D coll) script doesn't work, after I switched to edge colliders.
That's because edge colliders have no concept of being entered since they are just edges. Hmm, would it be possible for you to add a slightly smaller box collider than what you had before for your collision functions and leave the edge collider to remove the gap? The reason for using a slightly smaller box collider would be so you don't recreate the gap you removed.
I now tried this. it works after adding the box-collider. But when I change the box-collider size.x to 0.9. It is ignored again. I am making sure the player isn't touching the edge-collider, it is only used for closing the gap.
Don't know why I wrote x, I meant y :D. But sorry about that last comment, I forgot that after my cube suffers from a "fall", its behaviour will change. Changing the size.y caused it to do a tiny "fall". So no problem for now :)
Answer by sahilshubham · Dec 23, 2019 at 09:48 AM
Ok so here's the correct solution, go to Edit--> Project Settings--> Physics2D, there change the 'Default Contact Offset' value to the least value i.e. 0.0001.
The reason why that gap exists is due to the fact that there is a contact offset set to a value 0.01, which allows to colliders to not contact completely. I just figured it out, hope this helps!
While it's true that it does solve the "unrealistic gaps" reported above, there's another problem your solution creates.
The problem is when you jump and land, the sprite glitches a little through the floor, as if the Rigidbody2D component is set to Discrete and not Continuous.
Are there any other solutions to the issue?
Answer by superluigi · Aug 19, 2014 at 08:39 PM
It all depends on what you have. Either way it's an easy fix. If those are sprites or textures then what's happening is that they don't take up the whole image. If you used unity's own cube object for those blocks then just shrink the boxCollider2D of the top cube on the Y axis a little. Remember shrink the collider and not the ACTUAL object. After you make it smaller just move the collider upso that the top of the collider matches the cube. 2 easy steps and it's fixed.
I can push the block, no problem there. Your answer is what I used to do. Change the boxcollider.Y to 0.969, then it fits perfectly, but it will screw with my player sometimes when he want to jump on top of it. It can't find ground under his feet in some situations. But then you mentioned moving the boxcollider upwards. I tried to change boxcollider2D's center.y to 0.1, but now I can't push the box?
All the pushing is fine, when I don't use joint. I really can't use joints. I just need to fix this gap.
But it isn't I'm sorry. Your answer is what I used to do. But I realized it's not a good fix. The character's y-position will be smaller than it should be when he is walking over it after pushing it down.
It shouldn't be. I think you're complicating things. Either I don't understand what exactly it is you're trying to do, or this is an extremely easy fix. Literally like a 1 $$anonymous$$ute fix. Let me know exactly what it is you're trying to do.
Answer by narilearsi · Apr 09, 2018 at 09:30 PM
try fiddling with Min Penetration For Penalty value found in Edit->Project Settings->Physics 2D. A lower value of 0.001 rather than 0.01 fixed the problem for me.
Your answer
Follow this Question
Related Questions
Stack of RigidBody2D, Freeze X and Z, Odd Behavior 1 Answer
Is it possible to disable collisions for separate physics scene? 0 Answers
Unity Physics Collision Execution Order Error? 1 Answer
Platformer2D Player sticks to platform corners 0 Answers
increasing knockback of a rigidbody as received damage increases (like super smash) 1 Answer