How to find the tile ID of a tile the player is currently colliding with?

Tav54Tav54 Posts: 2Unconfirmed Member

I've been trying to solve this seemingly simple problem for the past 3 days now. I'm using a hexagonal grid with half offset. Like so:

All the tiles have collision shapes that look like this:


The player moves freely around the map (non-grid movement).

Here is the player's area code:
func _on_Area2D_body_entered(body: Node): if !body.is_class("TileMap"): return; var coords = body.world_to_map($Area2D.global_position); var tile = body.get_cellv(coords); print(tile);

With this code:

Going across right, it registers the tiles, but gives -1 (which means invalid tile) on every second one. This only occurs on the first row, maybe because I'm getting the row above it every second tile, since the grid is like a brick wall. On all other rows, the code works perfectly BUT only when going from left to right in a straight line.

Now going down one row, nothing is printed!

Coming all the way left, again nothing is printed.

I'd love if someone knew what was going on and how to implement a way to know the tile ID on the tile the player is currently colliding with like this.

Other info:

  • The size of the tilemap is 204x204 tiles
  • There are 300 unique tiles, each tile does something none of just for decoration.

Comments

  • bassebasse Posts: 21Member

    hi,
    are you using atlas texture? in that case, the get_cellv doesn't work, but you can get tile coordinates with:
    tilemap.get_cell_autotile_coord(x, y) ... yes, it says autotile in the name, but works for atlases too.

    .b

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file