Godot Version
4.4.1
Question
Hi! I’m creating a game with a TileMapLayer made of tiles of 16x16 and I want the player to walk around and be able to dig the tile they are in (say, by pressing A) or put a tile under themselve (say, by pressing E) to elevate themself by 1 tile (if that make sense, th’ats their way to climb).
Right now, I have a Node2D node called Game, and it contains a player (CharacterBody2D) that contains the camera (centered around the player). The Game node also contains a TileMapLayer that I get a reference of from the player script.
I have trouble getting the correct tile coordinate the player is on top of, for some reason…
My code right now (only the specific part that we’re interested in):
# Handle digging.
if Input.is_action_just_pressed("dig"):
# I tried this method first, but it does not erase the correct tile.
var tile_coords_1:Vector2i = tilemap.local_to_map(tilemap.to_local(to_global(position)))
# The second method I tried, but does not erase the correct tile either.
var tile_coords_2:Vector2i = tilemap.local_to_map(tilemap.to_local(global_position))
# Erase the tile_coors of method 1.
tilemap.erase_cell(tile_coords_1)
# Erase the tile_coors of method 2.
#tilemap.erase_cell(tile_coords_2)
print("Dig ", tile_coords_1, " / ", tile_coords_2)
The closest I get is with tile_coords_1: it erase a tile that’s kind of close to the player, but not that close. I suspect that’s a problem with the camera2D maybe? The weird thing is that when I move around, the distance between the tile_coords_1 and the actual player’s position is not the same. No matter the reason, it means that my method for getting the coordinates of the tile the player is in is incorrect.
Thanks for the help!