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.

in Engine by (12 points)

1 Answer

+2 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
by (10,207 points)

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!!!!!

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.
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 webmaster@godotengine.org with your username.

Categories