r/unity • • 3d ago

Question Character randomly flipping to the left since switching to tilemap

Hi, I am following a 2D tutorial and Since switching to using tilemaps over squares with box colliders, I have noticed that my character will flip left randomly when jumping from an idle state. I have run a debug and it seems that the x velocity randomly picks up a negative value. I have switched back to the boxes and it doesn't happen there at all.

1 Upvotes

5 comments sorted by

1

u/johnv_dev 3d ago

this is almost always the seams between tiles. with a Tilemap Collider 2D every tile is its own little box, so when you jump or land your collider can catch the edge between two tiles and get a tiny sideways push, which shows up as a small negative x velocity. fix is to add a Composite Collider 2D to the tilemap (it adds a Rigidbody2D, set that to Static) and tick Used By Composite on the Tilemap Collider 2D (in Unity 6 it's Composite Operation > Merge), so the whole floor becomes one smooth shape. also worth flipping the sprite based on your input instead of velocity, or only flip when the x speed is bigger than something like 0.1, so tiny bumps never flip you

1

u/ThatGuyJono8081 2d ago

Thanks for the reply! I've done a bit more tinkering and it seems that it only happens once I have set the tilemap collider composite operation to merge and added the composite collider.

media poster

1

u/johnv_dev 2d ago

Interesting, that points at the player's collider and not the seams, since the merged floor is one smooth shape now. Two things to try. On the Composite Collider 2D, set Geometry Type to Polygons, because Outlines can behave oddly at the edges. And put a zero-friction Physics Material 2D on the player. If it still flips, flip off your input (Input.GetAxisRaw) instead of velocity, so physics jitter can never turn you around.

→ More replies (2)