How can I get the player character to follow up/down ramps?

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

I’m having a hard time with a RigidBody2D (as Character) player-controlled object sticking to ramps. It either “jumps” down them or flies off the edge when moving up one.

The “simple” question is: what’s the best way of getting the player character to “stick” better to floors travelling up/down ramps? is the project in question if you want to check out the code. Uses v3.0.6. The current movement code is “inspired” (read: taken pretty much wholesale) from the 2D DCC Platformer demo.


:bust_in_silhouette: Reply From: Kevar

If you can wait, the 3.1 version of Godot will likely have what you need, see here (section about snapping):

Meanwhile, I think you can emulate this behaviour using some raycast to check if there is a ground close below your character and change its move to make him stick to it.
Then, you can check if the user is jumping or if the ground is too steep in order to deactivate this behaviour (otherwise, it may be stuck forever and will never fall or will teleport to the ground).

Hope it helps!

Well, the engine I’m writing is using RigidBody2D so won’t benefit from those changes, but I have access to another engine that is using KinematicBody2D and is having issues with collision raycasts. Shall try that approach with it and see what happens.

In any case, not going to be switching either engine over to 3.1 (the one using KinematicBody2D is being used in a game project) until it’s released.

Sslaxx | 2018-09-12 09:52

OK, sorry, I didn’t pay enough attention you were using RigidBody2D and not KinematicBody2D.

So, I think you can work with a raycast, get the normal of the collision, compute a tangent from it to get the “direction” of the ground and use this tangent as your “forward” unit for the next move, it should do the trick.

Looking at your code, I think you need to change the way it handles movement on the floor (line 173 to 186 in Instead of changing lv only on x, you can use the contact normal found earlier (line 149) to apply a change on y also.

Kevar | 2018-09-12 10:12