Player Sprite change directions random

My player change face directions ( i press left it change left, i press right he face right, but wen i stop pressing it goes facing left again) this only happens in some ground tiles.
I notice wen i dont use the Composite Collider 2D i dont have this problem, but then the enemies pass trough walls and i get stuck in some places.
The code seems to be all fine, i tried delete the platform ground and recreated but get same result.
Any idea what can cause this?
(Change facing directions on some tiles)

1 Like

Take all I’m about to say with a grain of salt since I’m not sure if this is causing your issue.

Based on what you are describing, this might be caused by colliders and the rigidbody’s velocity, let me explain with an image:

Examples (1)

The top example shows how the tilemap collider works, it creates really small gaps between each square, they are almost invisible, but if you zoom enough you’ll be able to see them. This might be causing your sprite to flip, when moving from square to square the character collides with that gap changing the rigidbody’s velocity.

The second image shows how the Composite collider works, it creates an edge, the collider is hollow, that’s causing your character to stuck or get through the colliders.

My solution to all of this is the third example but it requires manual work. Instead of using individual colliders per tile, or using the composite collider, I place big colliders that intersect with each other, this prevents the gaps and the hollowness.

This won’t fully fix your issue tho. The course’s code flips the sprite by reading the rigidbody’s velocity, which isn’t great because it is always changing, what you might want to do is create a system that flips the sprite on the player’s input; if the player pressed the left move key, flip the sprite to the left, if the player pressed the right move key, flip the sprite to the right, this gives full control of the sprite, which is far better in my opinion, especially when adding certain common platforming mechanics like transportation bands, slippery floors, or strong winds that move the character.

Hope this helps.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms