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

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

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.

:bust_in_silhouette: Reply From: ChristianSF

Does this shed any light on the matter?