0 votes

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

Godot version 3.5
in Engine by (57 points)

1 Answer

+1 vote
Best answer

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)
by (6,876 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.