Any ideas on how make an interactible button in a 3d space

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

Im trying to make a main menu where the Buttons can be pressed they are within the 3d scene so i was wondering how can i make them detect the mouse, frankly it seems pretty impossible to do this in godot but im asking anyway
Im not expecting from someone to give me the perfect answer i just need the logic to make this right

1 Like
:bust_in_silhouette: Reply From: Wakatta

Node Tree Setup

┖╴Button3D (StaticBody)
    ┠╴Viewport
    ┃  ┖╴Button
    ┠╴Shape3D (CollisionShape)
    ┖╴Sprite3D

Button3D Script

# Button3D.gd
extends StaticBody

func _ready():
    connect("input_event", self, "_on_Button3D_input_event")
    $Viewport/Button.connect("toggled", self, "_on_Button_toggled")
    $Viewport/Button.connect("pressed", self, "_on_Button_pressed")

func _on_Button3D_input_event(_camera, event, _position, _normal, _shape_idx):
	if event is InputEventMouseButton:
		if event.pressed:
			$Viewport/Button.emit_signal("pressed")
			$Viewport/Button.set_pressed(true)
		else:
			$Viewport/Button.set_pressed(false)

func _on_Button_pressed():
	print("pressed")

func _on_Button_toggled(button_pressed):
	print("toggled " + button_pressed )

Instructions

  1. Create a new ViewportTexture on Sprite3D and assign the Viewport in the above node tree
  2. Set the Viewport sizes to that of the Button node Rect size
  3. Change Shape3D shape to cover the Button area (BoxShape or PlaneShape)
  4. Connect the input_event signal of Button3D to the script above as seen in the _ready function
  5. Set Button toggle mode on
  6. Might have to set flipV on Viewport or Sprite3D node

Side Notes

There is actually a better way to get this done but if you’re lazy enough you can swap (Viewport, Button and Sprite3D) nodes for a Quad Meshinstance


┖╴Button3D (StaticBody)
    ┠╴Shape3D (CollisionShape)
    ┖╴Sprite3D (MeshInstance)