Can't click on buttons placed at the outside edges of viewports.

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

Hi everyone, just a disclaimer - I’m relatively new to coding in general so I’m still learning most of this stuff.

I’m trying to make a 3D scene with a bunch of different viewports around for the UI elements.
I’ve managed to get the viewports up and running for the most part but I’m having an issue where if I place a button outside of the very center of the viewport the clicks don’t register on it any more. I followed a guide to set up the code for it so there’s probably something I missed there.
Any help would be greatly appreciated!

func _unhandled_input(event):
	var is_mouse_event = false
	if event is InputEventMouseMotion or event is InputEventMouseButton:
		is_mouse_event = true
		
	if mouse_entered and (is_mouse_event or mouse_held):
		handle_mouse(event)
	elif not is_mouse_event:
		currentViewport.push_input(event,true)


func handle_mouse(event):
	mesh_size = currentMenu.mesh.size
	
	if event is InputEventMouseButton or event is InputEventScreenTouch:
		mouse_held = event.pressed
	
	var mouse_pos3D = find_mouse(event.global_position)
	
	mouse_inside = mouse_pos3D != null
	
	
	if mouse_inside:
		mouse_pos3D = currentArea3D.global_transform.affine_inverse() * mouse_pos3D
		last_mouse_pos_3D = mouse_pos3D
	else:
		mouse_pos3D = last_mouse_pos_3D
		if mouse_pos3D == null:
			mouse_pos3D = Vector3.ZERO
	var mouse_pos2D = Vector2(mouse_pos3D.x, -mouse_pos3D.y)
	
	mouse_pos2D.x += mesh_size.x / 2
	mouse_pos2D.y += mesh_size.y / 2

	mouse_pos2D.x = mouse_pos2D.x / mesh_size.x
	mouse_pos2D.y = mouse_pos2D.y / mesh_size.y

	mouse_pos2D.x = mouse_pos2D.x * currentViewport.size.x
	mouse_pos2D.y = mouse_pos2D.y * currentViewport.size.y
	
	event.position = mouse_pos2D
	event.global_position = mouse_pos2D
	
	if event is InputEventMouseMotion:
		if last_mouse_pos_2D == null:
			event.relative = Vector2(0,0)
		else:
			event.relative = mouse_pos2D - last_mouse_pos_2D
		
	last_mouse_pos_2D = mouse_pos2D
	
	
	currentViewport.push_input(event)
	

func find_mouse(pos:Vector2):
	var camera = get_viewport().get_camera_3d()
	
	var dss:PhysicsDirectSpaceState3D = get_world_3d().direct_space_state
	
	var rayparam = PhysicsRayQueryParameters3D.new()
	rayparam.from = camera.project_ray_origin(pos)
	var dis = 5
	rayparam.to = rayparam.from + camera.project_ray_normal(pos) * dis
	rayparam.collide_with_bodies = false
	rayparam.collide_with_areas = true
	
	var result = dss.intersect_ray(rayparam)
	if result.size() > 0:
		return result.position
	else:
		return null

func initializeMouse(newViewport: SubViewport, newMenu: MeshInstance3D, newArea3D: Area3D) -> void:
	if currentViewport != newViewport:
		currentArea3D.mouse_entered.connect(func(): mouse_entered = false)
		currentViewport.set_process_input(false)

		currentViewport = newViewport
		currentMenu = newMenu
		currentArea3D = newArea3D

		currentArea3D.mouse_entered.connect(func(): mouse_entered = true)
		currentViewport.set_process_input(true)
		

I verified the button works by placing it in the center, above the ones I currently have and progressively moving it upwards until it stops registering clicks.

I’ve tried messing with the sizes of things to see if that changes anything but it doesn’t seem like it.

Right now the scene for the Viewport and the Viewport itself is set to 1000x1000 px.
The MeshInstance3D is 10x10
The CollisionShape3D is also set to 10x10

Previously, I had set it to 1920x1080 and scaled the Nodes appropriately for 16:9 and it still had the same issue so i figured i’d just make it square for testing.

I’m not sure if there’s any more information I need to provide but please let me know if so.

I didn’t paste the entirety of the script only the parts I thought were relevant, just to save on some reading space. If necessary I can post it all.