system
September 14, 2022, 2:40pm
1
Attention
Topic was automatically imported from the old Question2Answer platform.
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
system
September 15, 2022, 9:18pm
2
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
Create a new ViewportTexture on Sprite3D and assign the Viewport in the above node tree
Set the Viewport sizes to that of the Button node Rect size
Change Shape3D shape to cover the Button area (BoxShape or PlaneShape )
Connect the input_event
signal of Button3D to the script above as seen in the _ready
function
Set Button toggle mode on
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)