+7 votes

I managed to detect multitouch using two Area2D._input_event (one area covers half screen), but now I can't detect a swipe properly.
Using Input.is_action_pressed() and Input.is_action_released() is great for detecting swipe, although apparently it's not capable of detecting more than one touch at a time.
So I tried detecting an InputEventScreenDrag, like this

func _on_GunArea_input_event( viewport, event, shape_idx ):
    if event is InputEventScreenTouch and not swiped:
        $Player.playerGun.shoot(event.position)
    if event is InputEventScreenDrag:
        gun_area_swipe += event.relative
    if abs(gun_area_swipe.x) > minimum_drag or abs(gun_area_swipe.y) > minimum_drag:
        gun_area_swipe = Vector2(0, 0)
        swap_guns()

Multitouch works fine with this approach, but swipe detection is not accurate (sometimes it calls swap_guns twice at the same time, sometimes it shoots after/before a swipe).
The main problem with this approach is that with InputEventScreenDrag I can't know how to detect when the swipe is over.
Is there a way to detect both multitouch and swipe in Godot 3.0?

in Engine by (515 points)

1 Answer

+6 votes
Best answer

After some struggling I managed to have both multitouch and swipe detection, here's what I found and how I did it:

  • the InputEventScreenTouch and the InputEventScreenDrag classes are good for detecting multiple touches, as you can see from the index property. You can also use TouchScreenButton as long as you don't need to detect a swipe gesture;
  • you can detect multitouch events using Area2D._input_event, just connect the corresponding signal to a function and write your code in there.

So I used two Area2Ds, connected the input_event signal and I wrote:

func _on_GunArea_input_event( viewport, event, shape_idx ):
    if event is InputEventScreenTouch and event.pressed:
        swipe_start = event.position
    if event is InputEventScreenTouch and not event.pressed:
        calculate_swipe(event.position)

Basically all you need is check if the event is an InputEventScreenTouch event type and check the status of the pressed property to see when the user starts touching and when he releases that touch. Then the calculate_swipe function checks if the input is a swipe and eventually emits a signal, as suggested in this post by another user.

I know this sounds pretty simple, but I'm sure that trying to check the pressed property did not work at all. Maybe it was because I was using an alpha build, maybe I was just burned out I missed something, but using this approach again worked with the stable. And it's so simple I can't believe how much time I wasted trying a workaround on my own!

Anyway, I hope this will help future newbies like me.

by (515 points)

Thank you it helped a lot. Especially I didn't know about the TouchScreenButton.

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.