0 votes

Hello. I'm new to Godot engine and I am trying to create a small infinite runner game.

I had the idea to move the ground instead of the player for no particular reason, I guess I liked this more :)

Now my question, how can I retrieve the world position of a tile while the tilemap is moving in the x direction?

My current code is the following

func _physics_process(delta):
    move_local_x(-1)
    print(left_pos())

func left_pos() -> Vector2:
    var used_cells = get_used_cells()
    var result = map_to_world(used_cells[0])
    return result

In my head this makes sense, but the value of left_pos() always stays the same when I print it to the console.

Is there something I am missing? Thanks for reading.

asked Jun 8 in Engine by Wofke (12 points)

1 Answer

0 votes

The coordinates returned by map_to_world are relative to the origin of the TileMap, not relative to the Viewport. They are world-coordinates, not global coordinates.

This should work:

func left_pos() -> Vector2:
    var used_cells = get_used_cells()
    var result = to_global(map_to_world(used_cells[0]))
    return result
answered Jun 10 by njamster (8,986 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 How to use this Q&A? before posting your first questions.