I'm making a simple android mobile game in Godot3.1. I would need to make a swipe detector that would determine if the player swiped and in which direction (left or right).
I tried this:
var swipe_start = null
var minimum_drag = 100
func _unhandled_input(event):
if event.is_action_pressed("click"):
swipe_start = event.get_position()
if event.is_action_released("click"):
_calculate_swipe(event.get_position())
func _calculate_swipe(swipe_end):
if swipe_start == null:
return
var swipe = swipe_end - swipe_start
if abs(swipe.x) > minimum_drag:
if swipe.x > 0:
_right()
else:
_left()
This works when you click with your mouse and swipe but it doesn't work when you play it on your android phone.
Any ideas?
That code only considers clicks and not screen touches, to correct that you have two options:
Go to Project > Project Settings > Input Devices > Pointing and turn on the Emulate Mouse From Touch option.
Or use the following code:
func _unhandled_input(event):
if event is InputEventScreenTouch:
if event.pressed:
swipe_start = event.get_position()
else:
_calculate_swipe(event.get_position())
instead of:
func _unhandled_input(event):
if event.is_action_pressed("click"):
swipe_start = event.get_position()
if event.is_action_released("click"):
_calculate_swipe(event.get_position())
With the last solution the code only accounts for screen touches so you wont be able to test it on PC unless you go to Project > Project Settings > Input Devices > Pointing and turn on the Emulate Touch From Mouse