Anyone who has tried making a tile-based game in Unity has probably encountered this issue, and I’ve seen many posts with people puzzling over why their player’s box collider is getting snagged on the environment.
The answer to this problem is nicely explained here: Ghost vertices - Box2D tutorials - iforce2d
I don’t know how to implement the solution they offer in the above tutorial, as they are using the Box2D physics engine, so I came up with my own solution. It’s clumsy and tedious, and there’s probably more reasons why it’s just plain bad, but on a basic level, it works:
I use a script that I found online to make objects snap to a grid which I apply to my individual tiles. Once my tiles are in place, I create an empty game object and put all of the tiles into it, just to keep each wall/floor/ceiling segment together. Then, I create another game object with a box collider that is big enough to cover all of the tiles in the segment. I apply the SnapToGrid script to the box collider, move it in place (over the tiles), and then put the collider into the WallSegment/FloorSegment/CeilingSegment game object-- again, to keep the related level pieces together. The SnapToGrid script causes crazy collision problems with my player, so I remove it from all of my tiles and colliders.
Pretty ridiculous, right? I know that you can purchase tools to make tilemaps in Unity, but I still feel like I’m at a skill level where I’d rather focus on doing what I can with the tools included in the free version of Unity. So, I’m willing to deal with the tedium as long as it works.
Here’s where it falls apart though: My player characters, which are almost square, need to be able to walk over each other smoothly. Here’s a screen-cap to explain what I mean:
The red player is currently controllable (selected = true) while the blue and yellow players are not. As you can see, the box colliders on my !selected players are a little too big-- the sprites just need to be adjusted so that the graphic doesn’t get smaller when they change form. Anyways, the problem is when the red player walks from the top of yellow to blue or vice-versa. Occasionally, it will become snagged on the lower player’s collision box.
A reoccurring solution that I’ve found online is to add four game objects with edge colliders to either each level tile or the player (I’d opt for the latter). I feel like this would solve the problem described above, as well as remove the need for my tedious level building process. I’ve read that edge colliders are a good way to go to stop this snagging problem, but nobody has described how they actually do it. When I add an edge collider to an object, I get a horizontal line. Nothing in the inspector seems to allow me to adjust the length of this line. I can manually change the size by holding Shift and click/dragging the end points, but this often leads to me accidentally changing the y-position of a point, and that leads to other problems. The other thing is I don’t know how I’m supposed to position these edge colliders in relation to my player. I could try to manually position my colliders exactly where each side of the box collider is, and then remove the box collider-- but again, that’s imprecise. It seems like the side edge colliders should be a little bit shorter, too. That way, when the bottom collider dips into another collider (like, one on the level, for example), the side edge collider stays above that point. Mmm… I’m having a hard time explaining this. If that doesn’t make any sense, I’ll make a diagram or something.
So what is the best way to go about replacing my players’ box colliders with four walls made of edge colliders? Any help is always appreciated.


