• 2D
  • How can I make a ladder that makes the player centered on the ladder?

So I'm making a 2D Platformer which needs ladders in the game, but when I search for ladder movement it mainly has what I call the "Super Mario style ladder" which allows the player to move anywhere on the ladder.

What I'm trying to do is have the player centered on the ladder as soon as they start to climb. I'm also doing ladders in terms of TileMaps and not as individual Area2D or StaticBody2D Scenes as it would be to time consuming.

What way would I go about this?

If you want to prevent the player from moving laterally off a ladder, you could do that in the TileSet Editor by defining collision shapes for the areas adjacent to the ladder.

19 days later

It will depend on how you are handling movement and physics in your game. I'm assuming based on the Mario comparison that your game is a 2D side scroller?

If you are using a statemachine for your character/movement then you simply switch state to OnLadder or something to that effect. The state itself would handle vertical movement code to allow you to ascend the ladder but simply not contain horizontal movement code.

Granted this assumes you are using statemachines and that you only have a single one, rather than multiple concurrent state machines for different movement etc... depending on how you have this set up you would need to adapt the solution.

I'm not sure how you are handling "ladder entry" logic but if you are not using state machines then you could potentially have a boolean called on_ladder that gets set to true as soon as whatever logic you have for being on the ladder triggers. You can then put a condition around your movement code to say something like

if !on_ladder:
    # do your normal horizontal movement code

In addition to this you could also while on the ladder set your horizontal X position to be the center of the ladder so that while you are on the ladder your position on the x axis is always the center of ladder.

So maybe something like:

if !on_ladder:
    # do your normal horizontal movement code
else:
	player.position.x = ladder.position.x # assumes both have their position anchored in their center rather than a corner

I'm not sure if the above will work for you or not as it will really depend on how you have the rest of your movement/collision/game logic setup, in addition I don't have any experience with tilesets yet so there might be additional caveats there but I think the basic reasoning I layout above should still generally carry over.