Reputation: 33
I am trying my best to build a mobile game in the godot game engine. And i have run into a problem. In a 3D game, how can i detect a mouse click on a specific object or rather where that object is on the screen. I do not understand how to use the control nodes in order to to this.
Upvotes: 3
Views: 5920
Reputation: 61
The rcorre's answer is right but just to give an update, in Godot4 that code looks like this:
func _ready():
input_event.connect(on_input_event)
func on_input_event(camera, event, click_position, click_normal, shape_idx):
var mouse_click = event as InputEventMouseButton
if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed:
print("clicked")
Upvotes: 3
Reputation: 7238
The easiest way to make a 3D object clickable is to give it a CollisionObject
(such as a StaticBody
) and connect to the input_event
signal. For example, to detect a left-click:
extends StaticBody
func _ready():
connect("input_event", self, "on_input_event")
func on_input_event(camera, event, click_position, click_normal, shape_idx):
var mouse_click = event as InputEventMouseButton
if mouse_click and mouse_click.button_index == 1 and mouse_click.pressed:
print("clicked")
The docs mention that touch events are similar to click events.
Note that input_ray_pickable
must be true
on the CollisionObject
(this is the default).
Upvotes: 4