How does one get tile position of moving tilemap

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

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 :slight_smile:

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.

:bust_in_silhouette: Reply From: njamster

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

Hello there! I’m fresh new with gdscript, and I’m developing a roguelike while learning. I found this solution so useful while using a tilemap instead of a sprite for player movement in a grid, and get the global position of your player. Thanks!!!

joseanjim | 2021-01-31 18:47