• 2D
  • Ladders... How do I snap my player character to the center of a ladder?

This seems like a common stumbling block for new developers, and, according to everything I've read and watched, I'm doing it backwards. My player character climbs up and down ladders and dismounts exactly how I want her to, but I want her to snap to the center of the ladder while she's climbing. How do I do that?

The "is-on-a-ladder checker" is built into the character, "Witch", in this project: https://www.dropbox.com/sh/gdeznmn90m9oz7b/AADhlCwEmoWskW1bdXg3gB-ta?dl=0

It seems every tutorial I check handles this functionality from the ladder object itself. Is there a way to have the character scene find the center of the ladder that she's climbing without the ladder sending that information to the character?

First you can narrow the ladder area so you'll be able to climb only in the center of ladder. Second, you can modify your script a little, I've attached modified script for testing and annotated it with # look here comments. For me it still needs some logic to avoid teleporting to the center of the ladder but it's a start.

@pkowal1982 said: First you can narrow the ladder area so you'll be able to climb only in the center of ladder. Second, you can modify your script a little, I've attached modified script for testing and annotated it with # look here comments.

That works. I also solved the problem by adding this line under if climbing: global_position.x = floor(global_position.x / 16) * 16 + 8

Though, this also requires the ladder's collision box to be narrower; otherwise, she sometimes teleports right off the side of it.

@pkowal1982 said: For me it still needs some logic to avoid teleporting to the center of the ladder but it's a start.

As for this, I agree, and I solved it by adding this: global_position.x = lerp(global_position.x, floor(global_position.x / 16) * 16 + 8, 0.5)

But, I think that weight is a little low.

Thanks for taking the time to look through my code! I appreciate it, and I hope it wasn't too messy. :dizzy:

Code was easy enough to modify it. :) As for aligning to ladder, physically correct attitude is probably to move first to the center horizontally (when you press up/down inside ladder area) and after reaching center move vertically. Not sure if this physically correct behaviour is worth of implementing, maybe lerp is good enough and this workaround resulting in increased velocity does not spoil anything.

I like the lerp, as it gives her a "clinging" feel without inhibiting her up/down motion, almost like she's in a hurry and "dragging" herself onto the ladder.

EDIT: But, I take back what I said about the weight. It was much too high!