Odd behavior for wall-crawling enemy

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

I have an enemy that crawls along walls and ceilings. When it hits a corner, an Area2D on its side detects it, changes its gravity to push it against the wall, changes its speed to move along the wall, and rotates itself. When it steps off a corner, an Area2D on its bottom corner detects that it has left a tile, changes its gravity to push it against the wall, changes it speed to move along the wall, and rotates itself. It mostly works fine, however sometimes when I test it, it will hit a wall/leave a corner and not rotate itself, and either keep pushing against the wall or fall backwards if it has left a corner because of gravity not changing. But sometimes when I test it without changing anything, it behaves as it should. Additionally, adding/removing a tile on its path will make it behave as it should. What could be causing this?

I’ve done some more testing, it appears to only happen when it is traversing a L-shaped segment that is two tiles on each side. That is, if it turns onto a platform that is two tiles long, and then traverses up a wall that is two tiles long, when it turns at the top of the platform it will fall back down to the bottom, as it appears that gravity has switched before its hitbox reached the next platform.

I seem to have fixed it, made it check for movement speed to be greater/less than or equal to 0 instead of just greater/less than.

JohnMaddenGodot | 2022-05-30 21:38

:bust_in_silhouette: Reply From: BigTuna675

Hi there, it’s going to be a little tough to help without seeing some more details about the scenes/collision shapes, but my initial question to you is what kind of collision shapes are you using? If you are using all rectangle-based shapes, you might get some weird edge case scenarios when one rectangle collider meets the edge of two adjacent rectangle shapes.

For example/illustrative purposes, if you have a grid-based cube world like minecraft, and you have a player which is a cube based collider, it might act weird or get stuck/bumpy sometimes when sliding over other cubes, since it is potentially colliding at multiple points where the edges meet. Using a capsule or circle collider in this example would help fix the issue.

Is it an option to change the collision shape of your enemy to a circle or a capsule? That might help with some of the edge detection. Good luck!