Buttons don't work in 3d

Godot Version

4.2.1

Question

I cant make a button to work in 3d, I went through a few giudes, and I can see it, but it doesn’t do anything. I’ve used SubViewport, the handle_input_locally is set, the script is realy easy (just changes text on pressed via a signal), I’ve tried resizing it, moving it closely, seting the handle_input_locally in the script, adding container, even asked AI, I am out of ideas. Pls help.

Do you want to display the Button on a 3D object or as a simple GUI element?

In the first case have a look at the GUI in 3D Demo.

In the second case, remove the SubViewport and put the Button in the root viewport.

2 Likes

Oh great! A thing that I want to undestand how to work with, fully functional and with zero explanation on how and why mine doesn’t! Exactly what I needed! But, you know, the opposite of that.

I just need a simple answer of what is missing in that examble for it to work. Not a mile of someones code with no idea how any of this does what it does.

https://godotengine.org/asset-library/asset/2807
the thing kaman showed is in the asset lib,
we can’t see your code to know why yours does not work.

this is the working that makes that work.

it seems to all work by using a area3d to tell the mouse location on the screen.
when the mouse enters the area3d a signal is emitted
it connects that signal to func _mouse_entered_area()
it flips the is_mouse_inside to true
then if theirs a click signal it uses _mouse_input_event() to convert the inside mouse to 2d viewport space.

if is_mouse_inside:
		# Convert the relative event position from 3D to 2D.
		event_pos2D = Vector2(event_pos3D.x, -event_pos3D.y)

		# Right now the event position's range is the following: (-quad_size/2) -> (quad_size/2)
		# We need to convert it into the following range: -0.5 -> 0.5
		event_pos2D.x = event_pos2D.x / quad_mesh_size.x
		event_pos2D.y = event_pos2D.y / quad_mesh_size.y
		# Then we need to convert it into the following range: 0 -> 1
		event_pos2D.x += 0.5
		event_pos2D.y += 0.5

		# Finally, we convert the position to the following range: 0 -> viewport.size
		event_pos2D.x *= node_viewport.size.x
		event_pos2D.y *= node_viewport.size.y
		# We need to do these conversions so the event's position is in the viewport's coordinate system.