Character getting stuck in tilemap

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By giantdwarf19

What I’ve done is that I flip the player depending on what arrow button the user has pressed. I use self.scale.x *= -1 to do that. The problem is that if I flip the player near a wall some collisionshape2d go inside the wall which results in the player being stuck. I have thought of a work around by adding an area2D on top of wall and using its onbodyentered signal to push the player away. But the problem with it is that I’ll have to do that with all the tiles. Is there any better way to solve this problem? Thanks in advance
Video showing the problem

it looks to me the collision area at the bottom is not symmetric relative to the filp axis, and when you flip, it goes inside the wall, stucking the character.
does it work if you fix the symmetry?

Andrea | 2021-01-16 13:10

No the problem wasn’t the symmetry, when flipping on the horizontal axis the sprite’s left foot would be inside the wall. And i’ve made the bottom collision in a way that it covers both feet so that way the collision would also go inside the wall. I’ve implemented a workaround to this problem by using raycasts to detect walls, so that way when i’m calling the flip function i’ll use the raycast to see if the character is next to a wall. If it is then after flipping i’ll increment/decrement the self.position.x of the character a few pixels away from the wall depending on whether the wall is on the right or left. I’ll be happy to hear some better solutions for this problem. Thanks

giantdwarf19 | 2021-01-16 17:00

when flipping on the horizontal axis the sprite’s left foot would be inside the wall.

yeah exactly, because it is not symmetrical.
just move the collision rectangle on the foots so that its center is positionated on the center of the character node, in this way the AABB rectangle will be positionated exactly in the same position when flipped, instead of slighlty translated.

you wont need any raycast then.

the other thing you can do is to flip the sprite only, and do not touch the collision poligon on the bottom.

Andrea | 2021-01-16 19:16

I implemented that and that has fixed the issue. The character is no longer getting stuck on walls. But the character is also no longer walking next to a wall because of the increased size of the collisionshape at the bottom. It looks as if there is an invisible box infront of the character (because of the increased size of the collsiion shape). This looks kinda odd don’t you think? I attached a link with the video below so you can get the idea.

Video Link

And also in the code i only flipped the sprite, head collision and body collision, didn’t touch the feet collision.

giantdwarf19 | 2021-01-17 05:27

well that’s a simple issue due to the character picture not being positionated exacty in the center of the sprite (the character is mostly on one side, the sword is on the other).
if you dont like this, simply move the sprite relatively to the node so that the character is roughly on the center.
Then, you can reduce the collision polygon on the bottom to the real dimension of the feet.
Just remember that collision node need to be positionated exactly in the center of the character node, only the sprite can be translated

Andrea | 2021-01-17 09:48

Ohh got it. It works now. Thank you so much for the help. Really appreciate it

giantdwarf19 | 2021-01-18 07:38