Can't collide with Area2D

Godot Version



Hi! I’m working on a simple 2D point n click -type game where I’m trying to use a collider to get metadata about which NPC the player is clicking; I would use this to start my correct discussion timelines.

The print “Detected” in the below code is working for stuff like StaticBody2Ds (with CollisionShape2Ds as children) in my scene as I’m getting my “Detected” print for the floor and the walls, but my NPC Area2D with CollisionShape2D as its child node is refusing to play along with this. Everything is on the same collision layer already and the relevant nodes are set to Pickable. Is it possible the code just wouldn’t work with Area2D?

I have a mouse control script, where I currently have the following:

func _unhandled_input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
		var space_state = get_world_2d().direct_space_state
		var query_params =
		var shape =
		shape.radius = 10
		query_params.transform = Transform2D(0, get_global_mouse_position())
		var results = space_state.intersect_shape(query_params)
		for result in results:
			print("Detected: ",
			if "collider" in result:
				var collider = result.collider
				if collider is Area2D and collider.metadata.has("npc_id"):  # Ensure the collider is an Area2D and has metadata
					var npc_id = collider.metadata["npc_id"]
					emit_signal("dialogue_area_clicked", npc_id)
					print("Dialogue Area Clicked for NPC: ", npc_id)
					break  # Break after the first successful detection

Any CollissionObject2D or CollisionObject3D has input handling support built-in. There’s no need to manually query the physics space for that. You can use the CollisionObject2D._input_event() method or connect to its CollisionObject2D.input_event signal to code your input interactions. You’ll need to enable CollisionObject2D.input_pickable for the method and signal to work.

Thank you!