Drag and Drop Feature Not Matching Mouse Position

Godot Version

Godot 4.2.2

Question

I’m making a tower defense game and I have a GUI where you can drag and drop towers from a menu. However the instance of the tower never matches the mouse position of the player.

The logic for placing the instances is within the gui panel’s script:

<
extends Panel

@onready var tower = preload(“res://basic_tower.tscn”)
var currtile

func _on_gui_input(event):
var temptower = tower.instantiate()
if event is InputEventMouseButton and event.button_mask == 1:
add_child(temptower)
temptower.global_position = get_viewport().get_mouse_position()
print(“left down”)
elif event is InputEventMouseMotion and event.button_mask == 1:
get_child(1).global_position = get_viewport().get_mouse_position()
pass
elif event is InputEventMouseButton and event.button_mask == 0:
print(“left up”)
get_child(1).queue_free()
var path = get_tree().get_root().get_node(“SceneHandler/MainWorld”)

	print(path)
	path.add_child(temptower)
	temptower.global_position = get_viewport().get_mouse_position()
else:
	if get_child_count() > 1:
		get_child(1).queue_free()
pass 
1 Like

try

get_global_mouse_position()

Instead…
I think this might work since your getting the mouse position local the the viewport which is fine if the camera doesn’t move
but I am new to this as well so don’t put all your faith in me

I’ve tried that already and it doesn’t solve the issue for some reason, but thank you

1 Like

did you try it without the get_viewport()?

yes, I did