2D Collision Pixel Problems

Hello.
I want to create some sort of bomberman clone and just managed to do the user control of the character ( instant velocity and instant stop when moving the character around ).
It is a topdown game but I still am using unity physics engine, because I want to use collision, so the player has a rigidbody and box collider. I created a tile prefab and procedurally generated some tiles on the map.

My problem is, that when moving the player on the outside of the tiles he sometimes gets stuck, even though the tiles are placed in a square on the map. The tile prefab also has a box collider.

Here you can see my problem: Just try going as close as possible to one tile and then moving up/down or left/right. So if you stand to the left of the bottom left tile, try moving up and then down again. You will (hopefully) get stuck at some point, even though the tiles (should) have the exact same x-coordinate ( or y-coordinate, when moving right/left ).

https://uncloaked.net/~teremy/unity/Webplayer.html

I generate the tilemap with 2 for-loops and pretty much just using i and j ( the variables of the for-loops ) as x and y coordinate of the tile.

Is this some precision error with floats? Also I think the character ( the black box ) is never actually hitting the tile, there always seems to be a space of about 1 pixel between them.

Any solution(s)? :frowning:

Thanks for your time.

You could try giving both the character and tiles a Physics Material with 0 dynamic and static friction (then have friction combine as minimum). Failing that, it reminds me of a problem I had making a tile based platformer.

In the end, what I did was write a tiling code for shapes. Based on the x and y scale of the shape, it would instantiate that many sprites along the shape. For example, if my tile sprite is 10x10 pixels and I want three in a horizontal line (along x), then I would make a box shape with scale (3,1,1) and a collider of size 10x10. The script would then see that the scale factor in the x direction is 3 and would create 3 tile sprites in the x axis along the shape. This results in one collider (which will not have any ‘bumps’ in it) with 3 tiles along it.
It can be a pretty complex code though; you have to take into consideration that depending on whether it’s an odd or even number that you want to instantiate, it would position the tiles differently. For example, with a 3x1 block (like previously mentioned) you would instantiate the first on in the middle, and the other two 10 pixels to the left/right. If it’s a 2x1 block, you would have to instantiate the first two tiles 5 pixels to the left/right of the center. And combining this with a block that isn’t X by 1 (e.g. 4x7), the script will gain complexity. I have my old script if you’d want to see it but it is very suited to what I was developing as it also considered rotated blocks and what not. If you can manage this yourself, then it would be a very rewarding experience; it will test your skills as an efficient programmer.

1 Like

I already solved my problem after a deeper research :).
I also found the suggestion with the physics material, though this didn’t solve my problem.

I’m not sure your solution will fix my problem. As you can see on my project in the webplayer, there are gaps between the tiles. The gaps aren’t that big, but they will be bigger, so you can walk between the tiles. The problem is that you may get stuck on a tile that should have the exact same y-coordinate and you’re just walking right ( or left… ).
This helped me understand the problem: Ghost vertices - Box2D tutorials - iforce2d

Now I’m just using edge-colliders to build a collision box on the player and everything works fine.
The physics material is also interesting because it helped me keep my velocity when walking 2 directions at once ( for example pressing S and D ) and colliding in one direction ( for example on the right ).

Now what’s next is to calculate the current Tile the player is on, which basically is just math, but it’s a bit more complicated in unity than it was in java, since I have to deal with unity unit scale…

Still thanks for your answer. I like to spend time to have things work most efficiently, I don’t exactly know how unity works internally though ( the collision detection for example ). I could just enable the colliders that make sense if the player moves…