+1 vote

Hey,

i would like to get objects under my finger on touch screen, Not only on press on that objects, but when i move finger and get collision with them. Is any way to do that nicely ?

For now, what i'm thinking about is dragging hidden object to x,y of touch, and collide it with the objects under

in Engine by (15 points)

2 Answers

–1 vote

Hey baribal!

I'd have an Area2D node that get its position updated with the InputEvent.SCREEN_DRAG, on which I'd subscribe to the body_enter or area_enter signals. You could do set_enable_monitoring on the Area2D node when InputEvent.SCREEN_TOUCH is fired, using the event.pressed boolean as an argument.

Here's an example:

func _input(event):
    if event.type == InputEvent.SCREEN_TOUCH:
        get_node("Area2D").set_enable_monitoring(event.pressed)
    elif event.type == InputEvent.SCREEN_DRAG:
        get_node("Area2D").set_pos(event.pos)
by (62 points)

You might need to point out to add the setprocessinput(true) in the area 2d _ready function too

+1 vote

You can detect mouse and touch input on Area2D or other physics nodes (as you would on Controls) if you enable the pickable property. If you do so, you will get regular input events from them by connecting the input_event to your custom function http://docs.godotengine.org/en/stable/classes/class_collisionobject2d.html?highlight=input_event#signals

by (27,664 points)

Hi there, jumping in on this thread as I'm trying the same thing.

I made an Area2D pick-able.
I then connected the input_event signal to my own function as so:

func _on_Area2DL_input_event( viewport, event, shape_idx ):
    print("viewport: ", viewport)
    print("event: ", event)
    print("shape_idx: ", shape_idx)
    print("============")

I have emulate_touchscreen turned on and when I click on the Area2D none of the print statements show anything (as in they don't trigger at all).

I'm guessing I'm misunderstanding this? Can you or anyone explain what I'm doing wrong?

Thank you.

EDIT: I added a collision shape (rectangle) as a child of the Area2D and it worked. I don't fully understand that but I'll have a think and a read to make sense of it all.

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 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 webmaster@godotengine.org with your username.