Mouse click event has unexpected position

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AlexG

On the top level I have a Node2d where I want to capture the mouse position on a click event. However, something is messed up and I have no clue what. The following GDScript is attached to the node.

extends Node2D

func _unhandled_input(event):
	if event is InputEventMouseButton:	
		print(event.position)
		# (514, 309)
		print(event.global_position)
		# (514, 309)
		print(get_global_mouse_position())
		# (1, 4.5)

If I click close to the center (0, 0) I get for event.position and event.global_position
coordinates which are totally off. However, get_global_mouse_position() works fine.
All properties for the node are on default. What could cause such behaviour?

Keep in mind get_global_mouse_position() is not in screen space, it is relative to the canvas your node is in. i.e if you have a zoomed/displaced camera it will acount for that.

On the other hand I think event.position and event.global_position are not using the same coordinate space, but I don’t have Godot around to test this.

Zylann | 2022-02-14 13:54

@Zylann Thanks for commenting! The “problem” was indeed a camera node.

AlexG | 2022-02-19 14:34