Flip not working on Tilemap

Hello there, I’m pretty confused. I followed all the instructions, but when my character stops on a tilemap, it automatically flip on the other side. Strangely this doesn’t happens on every other platforms, like a rectangle with a collider. Anyone can give me a little help?
Thank you so much!


What do you mean by ‘flipping’?

The tiles don’t flip unless there is code that makes them flip, or if there is a Rigidbody2D attached to the tilemap, which would make the physics simulation move the tilemap.

Have you already checked that?

No, not the tiles. The character. When it moves, for example, on the right, he faces the right side, but when he stops, suddensly he flips on the left. Strangely, this happens only when he moves on a tilemap. This is the code I used (which is the same of the course; also, I use it in the Update method) :

void Flip()
bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
if (playerHasHorizontalSpeed)
transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);

Could you please share a screenshot of the tilemap collider? Maybe there are gaps which cause the player to flip.

Here they are. Thank you so much in advance.

Could you also share a screenshot of the gaps? At the moment, I don’t know if we mean the same. My answer won’t be helpful if the gaps are not the gaps I was referring to.

If there are gaps:

  • The Geometry Type of the CompositeCollider2D must be set to ‘Outlines’, not ‘Polygons’.
  • Disable the TilemapCollider2D component and enable it again to make Unity recalculate the collider shape.

Are the gaps gone?

As you can see there are no gaps, and the collider is set in the way you said. Still, it behaves strangely.

Very odd. How does the collider shape of the character look? Is there anything that could make Unity interpret a situation as ‘exits collider’.

Check your console. Are there any warnings or error messages? Even if messages do not seem to have anything to do with your current problem, there is a chance that the problems cause unexpected side-effects which cause the player to flip for no apparent reason.

For testing purposes, disable all game objects but the ground (which causes the problem) and the player. Then test your game again to see if the problem is indeed caused by the ground and not by something else.

Then disable the ground and add a BoxCollider2D which matches the collider in your screenshot. Test your game again to see if you get the same problem.

If you don’t get the same problem, create a new tilemap layer, paint the area where the BoxCollider2D is, add a TilemapCollider2D and a CompositeCollider2D. Then remove the BoxCollider2D (game object). Test your game again.

This way, you’ll hopefully be able to narrow the problem down a bit further. Since the white platform in your first screenshot seems to work correctly, you want to figure out what the difference between the white platform and the ground is. Sometimes, differences are not visible immediately, and sometimes, Unity has got internal problems.

Privacy & Terms