Rigidbody getting stuck on tiled wall

Hello, I’m currently having issues with this, not sure how to solve it:

Setup:
Top down, tiled
Character with 2D box collider+ rigidbody, moved by changing velocity(works fine).
Walls are made of tiles with 2D box colliders, everything has 1x1 unit size so there definitely is no gap between the wall tiles.

The problem:
Every now and then when moving the character along the wall he gets stuck on it.
I can get him unstuck by turning the direction he is facing (scale*-1) without changing the movement direction.
Anyone got any hints on what could be causing this ?

hi yes the same thing happened to me.

Just assign a Physics material to your wall and that should fix it.

Dilmer

3 Likes

Hm… thanks for the tip. That doesn’t seem to fix it for me though, also tried playing around with the Friction and Bounciness values but he always ends up getting stuck.

If I change the collider to a circle collider the problem goes away, but then my character starts slowing down when running diagonally against the wall even though there is no friction. (probably the forces getting calculated differently)
Also the circle collider isn’t the ideal shape for what I’m doing.
I might try combining circle and box colliders.

If anyone has another way to solve this I’d appreciate it.

I setup my character just like is shown in this video:

http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers

The speaker does use two colliders for the character and that’s what I did with my character.

Good luck
Dilmer

I think it’s a problem with the 2d box colliders. When using tiles as floor the character occasionally bumps up, even when the colliders are all evenly lined up without seams and even with a circle collider. This only seems to happen when you have many colliders placed next to eachother.

As temporary fix I’m using a single box collider stretched out over all my tiles, but this wouldn’t be a good idea if you have an in-game level creator.

Its definitely an issue for people building tile based levels with box colliders. Im not Sure what happened between 4.3 and 4.3.1 but I swear dilmers suggestion worked on tiles at one point.

Yeah I would have done that too but since I’m generating levels at run time it’s sadly not an option I think .

1 Like

I actually have this problem even with a circle collider against a frictionless wall. The wall is not tiled, but it a Polygon Collider. The sticking points are the vertices between tris.

The work-around for me was that I constantly animate the radius of the circle collider between 1 and 1.1 . This slow pulse/movement seems to prevent any sticking and is essentially unnoticeable. I suspect it’s not great for performance though.

Thanks, the Physics Material worked for me.

I am working with Toolkit2D combined with Unity 2D Physics.
Part of my walls are actually tiles generated with Toolkit2D TileMap. Others are Toolkit2D sprites with 2D box colliders (Tk2D lets you set appropriate colliders in the Sprite Collection Editor and automatically adds them the sprite objects).
By using a custom Physics 2D Material with Friction = 0 and Bounciness = 0, the friction effect disappears. Using the default value of 0.4 for Friction gives the same result as using None Material (the default physics 2D material), i.e. a slowdown when moving diagonally (not when grazing the walls).

To apply the change to the whole TileMap of Tk2D, you have to go the settings and change the physics material used for the layer you want, then commit. I guess there are similar global parameters for other TileMap systems.

For those who still have issues with friction with the custom material, check your move algorithm. If you are normalizing speed for diagonal directions, moving diagonally against a wall makes your character move sqrt(2) slower in the projected cardinal direction. However, a friction of 0.4 makes it move considerably slower, so if your character is really slow it is not because of speed normalization (I though that at first, then disabled normalization and could still see the issue).

If Unity 2D Physics tuning alone is not enough to solve the issue, you may need to refine your move controllers, which may be more or less complicated depending on your game. You could, for instance, make your character detect walls beforehand and only move in “free” directions.

Hope that helps someone.

2 Likes

Just to save you guys some trouble, if you use Edge colliders instead, it gets rid of the issue. Just make 4 empty-game-objects a child of your tile, each with an EdgeCollider2D, and 2 of them rotated to form the walls. This way you’ll also be able to tag the walls as walls, floors as floors and ceilings as ceilings… And you won’t run into the ghost vertices problem that causes you to get stuck as well.

Edge-Colliders also allow you to alter their shape and add more points, by holding down Shift and click+dragging anywhere along the edge.

2022 Edit: Tilemap package exists now which is generally a better option. You can use a “Composite Collider 2D” which will merge all your tile box colliders into one continuous collider that doesn’t have “ghost vertices”, but also retaining the benefit of a box collider which will push objects out from inside of it, unlike an edge-collider which has no “volume”.

If your game can’t really be tile-based, then the edge-collider is still the best option.

11 Likes

Alternatively, you could use an edge collider for your character instead of the terrain tiles. That’s what I did. I added some extra vertices and shaped into a box so that it would still give the all-around collision I wanted. (I also tried using a polygon collider, but for some reason it got stuck just as much as the box collider had)

4 Likes

Yeah, I was having this problem and putting the edge collider on the character worked like a charm.

I’d try applying force to the rigidbody instead of directly changing the velocity. 2D physics engines tend to get “confused” if you do that. Also, make sure to not move bodies (by applying forces or modifying velocity) in the Update(). That should be done in FixedUpdate() if you need to use continuous force.

If the rigidbody is moving very fast, you may need to use Continuous Collision Detection, but that’s for extreme cases.

Nah, it’s an issue with Box2D and some other engines. Edge colliders are the solution.
http://www.iforce2d.net/b2dtut/ghost-vertices

3 Likes

Ran into same issue, where two box colliders with rigidbody were getting stuck into each other ! on one rigidbody I just surrounded it by edge collider instead of Box Collider ! Voila !! Problem solved :slight_smile:

1 Like

Physics Material Worked perfect for me! Thank you :slight_smile:

Thanks! This worked great.

The edge collider fix here is working well for me so far!

The problem with Edge Colliders is that physics overlap can’t see them. If you are using Physics Overlap for grounded calculations your player wiil never be grounded.

Running a box collider over other box colliders stacked together seems to make the box get stuck. (this is particularly annoying with tile based 2D games)

The technique for using a circle collider for feet and a box collider for head works until you want 1-way platforms or ladders. Then one of the colliders will get stuck.

In any case, if you want boxes / crates etc, giving them circle colliders makes them slide about with bad friction (even with friction ramped up to the max).

Not sure of a work around, Unity would have to change the way Box2D works. But if we are to make 2D platformers in Unity, there should be some fix in the works as they will come accross the same issues too.

A 2D character controller by @MelvMay is in the works but no date on it’s arrival. It’s a lozenge shaped collider like the 3D character controller. This seems to fix the issue for players or enemies but as highlighted above, the box collider on moving crates would still get stuck in tiles, also it’s not part of Unity just yet.

The CapsuleCollider2D isn’t a character controller and it won’t solve the issue of catching corners on discontinuous collider surfaces, it’s prone to catching corners just like any other collider.

I am however working on a solution that allows you to group and therefore merge colliders together but this is a WIP.

I created IsTouching and IsTouchingLayers to detect if you are currently touching other colliders and in a lot of cases this can be used for an ‘Is grounded’ check. It’s also super fast as it only checks existing contacts and doesn’t perform any intersection tests. It also isn’t just an approximation of whether you’re touching something, it’s actually accurate and real contacts and not some offset overlap.