0 votes

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?

https://github.com/Sslaxx/GodotSonicEngine 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.

Thanks!

in Engine by (24 points)

1 Answer

0 votes

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

https://godotengine.org/article/godot-31-will-get-many-improvements-kinematicbody

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!

by (19 points)

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.

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 player_generic.gd). Instead of changing lv only on x, you can use the contact normal found earlier (line 149) to apply a change on y also.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.