Scene dragged with mouse doesn't account for camera zoom, and spawns in the wrong spot

Godot Version

Godot 3.6

I’m trying to instance a tower on mouse click from the UI in the bottom left, which can then be dragged and placed on the screen.

The issue is that the towers spawn where the UI is in the 2D Workspace, and doesn’t move the same speed as the mouse if the camera is zoomed in our out when being dragged around.

extends Button

var tower = preload("res://-- Towers --/Scenes/TemplateTower.tscn")
var tempTower

func _on_Button_gui_input(event):

	if event is InputEventMouseButton and event.button_mask == 1:
		#Left Click down
		tempTower = tower.instance()
		get_tree().current_scene.add_child(tempTower)
        tempTower.global_position = event.global_position
		pass

	elif event is InputEventMouseMotion and event.button_mask == 1:
        #Left Click drag
		tempTower.global_position = event.global_position
		pass

A lovely samaritan helped me out.

I changed

tempTower.global_position = event.global_position

to

tempTower.global_position = get_viewport().canvas_transform.affine_inverse() * get_viewport().get_mouse_position()

and it worked flawlessly!