0 votes

Hello folks,
I'm trying to detect a swipe gesture using Godot 3.0 alpha2, but I've got no clue where to start from. Searching for 'swipe' in the current docs gets no results and I'm so not familiar with this kind of input.
So, can anyone give me a lead? Where to start from? Or is there already something I can use?

in Engine by (515 points)

I see there are some helpers on Github to detect swipe, such as this for Godot 2.x : https://github.com/arypbatista/godot-swipe-detector
There shouldn't be much differences about this in Godot 3.0, some functions are renamed and look different but the functionnality is the same. Also remember it's still alpha^^

I actually know the existence of swipe detector, and I tried using that before asking this question.
I downloaded it and opened in 2.1.4, then used the "export to 3.0" tool. Of course I knew this was probably not working, so I opened the exported version with 3.0 and tried fixing every error, which I couldn't do because there are some built-in scripts I couldn't edit if some scenes were not loaded.
So I isolated the library by deleting the examples folder and only keeping the addons one, then I created another scene with just a Node2D to test it, I instanced SwipeDetector.tscn as a child of my new scene, created a script and connected a function to the signals to get the input points, but nothing.
The engine gives me no error, but no output either! So it's pretty much not working and I've got no clue on how to fix it :(

1 Answer

+5 votes
Best answer

I have this in an autoload:

signal swipe
var swipe_start = null
var minimum_drag = 100

func _unhandled_input(event):
    if event.is_action_pressed("click"):
        swipe_start = get_global_mouse_pos()
    if event.is_action_released("click"):
        _calculate_swipe(get_global_mouse_pos())
        
func _calculate_swipe(swipe_end):
    if swipe_start == null: 
        return
    var swipe = swipe_end - swipe_start
    print(swipe)
    if abs(swipe.x) > minimum_drag:
        if swipe.x > 0:
            emit_signal("swipe", "right")
        else:
            emit_signal("swipe", "left")

It just emits a signal with one argument that can be "right" or "left". If you don't know how to start maybe this helps.
Basically, if you don't care about the whole curve, your swipe is swipeend - swipestart where swipestart and swipeend are the return value of getglobalmouse_pos() when the user touches the screen and when she lifts her finger, respectively.

by (116 points)
selected by

So simple and so perfect. That's exactly what I needed to get started, thank you soooooo much!

Also, can I ask why using _unhandled_input instead of _input?

Perfect, thank you.

Just to add there, but that handling of the input could just be over an appropriate control node.

I was wondering though, because godot already has specific types of InputEvent with mouse motion and swipe motion, I would like to see them in use.

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.