Godot Version
v4.4.1.stable.official [49a5bc7b6]
Question
I’m trying to make a Tower Defense game and I want the towers that I place to be on the tilemap, but since they’ll have quite a bunch of functionality like being selected, transformer, rotated etc, I did not want to make them into tiles on the tile map.
I have a base layer that’s just green tiles then I add small decorations on top, the problem I’m encountering is that when I try to spawn things, they’re always one pixel off unless I do:
position.x- 1
.
Usually I’d just do that then go on with my day but getting sprites to and snap to the tiles was already hard enough, so I’m just curious about what’s causing this.
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
var local_pos = tilemap.map_to_local(tilemap.local_to_map(get_global_mouse_position()))
if local_pos.abs().x > MAX_WIDTH or local_pos.abs().y > MAX_HEIGHT or pathmap.get_cell_source_id(pathmap.local_to_map(local_pos)) != -1:
return
spawn_tower(tower_scene, local_pos)
func spawn_tower(tower_scene: PackedScene, pos: Vector2i):
var tower: Node2D = tower_scene.instantiate()
tower.position = Vector2i(pos.x - 1, pos.y) # offset because otherwise it doesnt align
add_child(tower)
The reason I call both map to local and local to map is to make it snap to the grid. By the way tilemap in this case is my base tile map layer, which is just green tiles.