0 votes

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

func onArea2Dbodyentered(body):
if body.name == "Player":
getnode("../Player").ladderon = true

func onArea2Dbodyexited(body):
if body.name == "Player":
getnode("../Player").ladderon = 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?

Godot version 3.3.2
in Engine by (47 points)

1 Answer

–1 vote
  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

by (1,081 points)
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 Frequently asked questions and 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 [email protected] with your username.