I have a sort of “halfpipe” setup and ideally I’d want my 2D Rigidbody to slide along the wall and make a perfect U shape. What happens instead is that it intermittently contacts with the wall, and as it exits the left side “ramp” it makes contact with the lower box collider at the same time as the edge collider and gets kicked up.
I have tried using a polygon collider instead, but this causes similar, more pronounced issues.
Using the adjacent start points improves things (they are on in the example), but extending them (or the outermost points of the edge) makes no difference.
In this test case I’m letting the ball fall along the wall for an ideal setup, but it may enter at any angle.
I had some luck improving this by tweaking the Default Contact Offset, but it didn’t fix it entirely and caused the ball to stray too far from the wall when colliding. Plus it has a scary warning about being careful when changing it.
I’m absolutely open to hacking this somehow, but I do need to maintain a full simulation as there might be other things to collide with as this slide happens. There’s also no particular reason this needs to use an edge collider, if anything else could give better results.
Friction is set to .05 on the walls, zero on the ball. Bounciness is .8 for the wall, 1 for the ball.
In summary, I query for a nearby EdgeCollider2D, work out if i’m close enough and moving approximately perpendicular to the “surface”. If so I correct the velocity to be exactly perpendicular. Then, profit!
One of the trickier issues turned out to be Collider2D.ClosestPoint() not returning quite what I expected.
The code is like, half comments, so reading that top to bottom should give a pretty good picture of what’s going on.
And to clarify what the issue was, it all starts to go wrong at about the midpoint of the rounded corner. The ball penetrates into the corner a bit too much just before and stops contacting for a frame or two, then makes a bigger contact, gets pushed away harder and then does that little skip as it digs into the super thin part of the edgecollider and simultaneously hits the box collider that’s just below.
I found that decreasing the step size to be like 1000hz helped a little bit, but that seemed like way overkill. Iteration counts for resolving didn’t make much difference.