Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting click/touchscreen input on a 3d object inside godot

Tags:

godot

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.

like image 808
Partin22 Avatar asked Oct 20 '25 01:10

Partin22


1 Answers

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).

like image 130
rcorre Avatar answered Oct 22 '25 07:10

rcorre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!