Godot Version
4.3
Question
Im attempting to make a 2d tower defense in godot 4.3, and I have a camera2D for wasd movement in game. I also have a drag tower preview attached to the mouse so the player can see where it will be built if clicked. However whenever I move the camera in game, it offsets the drag tower preview texture by that much rather than it staying on the mouse like it should. I cant seem to figure it out.
here the script attached to my UI:
extends CanvasLayer
func set_tower_preview(tower_type, mouse_position):
var drag_tower = load(“res://Scenes/Towers/” + tower_type + “.tscn”).instantiate()
drag_tower.set_name(“DragTower”)
drag_tower.modulate = Color(“ad54ff3c”)
var control = Control.new()
control.add_child(drag_tower, true)
control.set_position(mouse_position)
control.set_name("TowerPreview")
add_child(control, true)
move_child(get_node("TowerPreview"), 0)
func update_tower_preview(new_position, color):
get_node(“TowerPreview”).set_position(new_position)
if get_node(“TowerPreview/DragTower”).modulate != Color(color):
get_node(“TowerPreview/DragTower”).modulate = Color(color)
and heres the game scene script:
extends Node2D
var map_node
var game_speed = 1.0
var fast_forward_speed = 2.0
var mage_tower_cost = 100
var cannon_tower_cost = 160
var is_paused = false
var build_mode = false
var build_valid = false
var build_location
var build_type
var build_tile
@onready var mouse_pos = get_global_mouse_position()
@onready var ui = $HUD/UI
@onready var mage_label = $HUD/UI/M/BuildBar/M/PC/HB/MageTowerButtonPanel/mage/MagePrice
@onready var cannon_label = $HUD/UI/M/BuildBar/M/PC/HB/CannonTowerButtonPanel/cannon/CannonPrice
@onready var gold_label = %GoldLabel
@onready var health_label = %HealthLabel
func _ready():
map_node = get_node(“Map1”)
gold_label.text = str(GameData.gold)
health_label.text = str(GameData.health)
for i in get_tree().get_nodes_in_group(“build_buttons”):
i.pressed.connect(initiate_build_mode.bind(i.name))
func _process(delta: float) → void:
if build_mode:
update_tower_preview()
gold_label.text = "Gold: " + str(GameData.gold)
func _unhandled_input(event: InputEvent) → void:
if event.is_action_released(“cancel”) and build_mode == true:
cancel_build_mode()
if event.is_action_released(“click”) and build_mode == true:
verify_and_build()
cancel_build_mode()
func initiate_build_mode(tower_type):
if build_mode:
cancel_build_mode()
build_type = tower_type + “_tower”
build_mode = true
ui.set_tower_preview(build_type, get_global_mouse_position())
func update_tower_preview():
var tower_exclusion = map_node.get_node(“TowerExclusion”)
var mouse_position = get_global_mouse_position()
var current_tile = tower_exclusion.local_to_map(mouse_position)
var tile_position = tower_exclusion.map_to_local(current_tile)
if tower_exclusion.get_cell_source_id(current_tile) == -1:
ui.update_tower_preview(tile_position, "ad54ff3c")
build_valid = true
build_location = tile_position
build_tile = current_tile
print(mouse_position)
else:
ui.update_tower_preview(tile_position, "adff4545")
build_valid = false
func cancel_build_mode():
build_mode = false
build_valid = false
get_node("HUD/UI/TowerPreview").free()
func verify_and_build():
if build_valid and build_type == “mage_tower”:
if GameData.gold >= mage_tower_cost:
var mage_tower_instance = load(“res://Scenes/Towers/mage_tower.tscn”)
var new_mage_tower = mage_tower_instance.instantiate()
new_mage_tower.position = build_location
map_node.get_node(“Towers”).add_child(new_mage_tower, true)
map_node.get_node(“TowerExclusion”).set_cell(build_tile, 5, Vector2(1, 0))
GameData.gold -= mage_tower_cost
gold_label.text = str(GameData.gold)
else:
print(“Not Enough Gold”)
elif build_valid and build_type == “cannon_tower”:
if GameData.gold >= cannon_tower_cost:
var cannon_tower_instance = load(“res://Scenes/Towers/cannon_tower.tscn”)
var new_cannon_tower = cannon_tower_instance.instantiate()
new_cannon_tower.position = build_location
map_node.get_node(“Towers”).add_child(new_cannon_tower, true)
map_node.get_node(“TowerExclusion”).set_cell(build_tile, 5, Vector2(1, 0))
GameData.gold -= cannon_tower_cost
gold_label.text = str(GameData.gold)
else:
print(“Not Enough Gold”)
func take_damage():
pass
func game_over():
pass
if someone could assist me in solving this issue I would be most grateful.
Thank you!