How to find the middle of the ladder?

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

I’m making a 2D platformer. I added ladder to my project.
Area2D → TileMap ->tileset(ladders.tres). Area2D has a script:

func _on_Area2D_body_entered(body):
if body.name == “Player”:
get_node(“…/Player”).ladder_on = true

func _on_Area2D_body_exited(body):
if body.name == “Player”:
get_node(“…/Player”).ladder_on = false

My hero can jump up the ladder and jump off. But I cannot implement centering the hero along the x-axis on the ladder. How to move the hero to the center of the ladder along the x-axis? There is a scene where I created a tileset (Convert to… TileSet). Node2D → Sprite → StaticBody2D → ColisionShape2D → Position2D. My idea is, if the ladder sends a signal to the hero and ladder_on == true changes and if at this moment the up button is pressed, then gravity will be canceled and the global position of the hero will take on the x Position2D value of the ladder. Thus, the hero will move to the center of the ladder. But how does the hero get x Position2D ladder? maybe there is another way to move the hero to the center of the ladder?

:bust_in_silhouette: Reply From: juppi
  1. Give the Player a class name:
    class_name Player

  2. Add a instance variable in the player script:
    var ladder_position_x: float = 0

  3. Modify the signal of the area:
    func _on_Area2D_body_entered(body: Node): if (body is Player): (body as Player).ladder_position_x = position.x