- Edited
Hi, with the code below I face a issue when clicking on the tile to place the objects, when clicked with the right mouse button on the tile the objects spawn on the above tile or below tile depending where I click on the tile.
I tried adding a offset to the y pos but the results vary?
The tilemap is also set to the isometric position, so maybe I am missing something.
`extends TileMap
const ground_layer = 0
const objects_layer = 1
const ground_tiles_atlas_id = 0
const ground_1_tile = Vector2i(0, 0)
const ground_2_tile = Vector2i(1, 0)
const wall_tile_atlas_id = 1
const wall_tile_atlas_coords = Vector2i(0, 0)
#const building_tile_atlas_id = 2
#const building_tile_atlas_coords = Vector2i(0, 0)
@export var horizontalSize : int = 16
@export var verticalSize : int = 32
#var cell_is_empty: bool = true
Called when the node enters the scene tree for the first time.
func _ready():
for i in range(0, horizontalSize):
for j in range(0, verticalSize):
set_cell(ground_layer, Vector2i(i, j), ground_tiles_atlas_id, ground_1_tile)
#cellisempty here
func _input(event):
if(Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)):
var mousePos = get_local_mouse_position()
var tileMapPos = local_to_map(mousePos)
var data = get_cell_tile_data(ground_layer, tileMapPos)
var coordsOfClickedTile = get_cell_atlas_coords(ground_layer, tileMapPos)
print(tileMapPos)
else:
if(Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)):
#print(right mouse press)
var mousePos = get_local_mouse_position()
var tileMapPos = local_to_map(mousePos)
var data = get_cell_tile_data(ground_layer, tileMapPos)
var coordsOfClickedTile = get_cell_atlas_coords(ground_layer, tileMapPos)
#var offset = 0.32
#tileMapPos = Vector2(tileMapPos.x, tileMapPos.y + offset)
#add wall if cell empty
set_cell(objects_layer, tileMapPos, wall_tile_atlas_id, wall_tile_atlas_coords)
`
Heres the project if anyone can see the solution?