0 votes

Hi all.

Just a little question regarding TileMap. I'm trying to get navigation working, but I need to get the centers of my tiles in Global Coordinates so the vehicle can move to each one on its way to the destination tile. I also need to convert the Global Coordinates of the mouse cursor to the nearest Cell position on the TileMap, so I can set a destination tile.

I ask this because my tank goes flying off with a positive Vector2 at Ludicrous Speed. (Well, not exactly, but if it could, it'd be plaid.)

Oh, and it makes a fuss if the tile's Global Position is negative in either X or Y.

in Engine by (197 points)

1 Answer

+1 vote
Best answer

TileMap has functions for this:

If you have a tile coordinate, you can find the position in pixels of the tile's center like so:

# tile_pos is your tile coordinate
var tile_center_pos = map_to_world(tile_pos) + cell_size / 2

For your other question, the reverse is also possible:

var mouse_tile = world_to_map(get_global_mouse_position())

See TileMap docs for details)

by (20,385 points)
selected by

Ah, got it. Thank you.

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.