Skip to content

Instantly share code, notes, and snippets.

@eduardonunesp
Created April 21, 2020 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduardonunesp/725ae44b1ac8302dee3b0f079a7ad905 to your computer and use it in GitHub Desktop.
Save eduardonunesp/725ae44b1ac8302dee3b0f079a7ad905 to your computer and use it in GitHub Desktop.
Swipe Detector Godot
extends Node2D
signal swipe(direction)
var swipe_start = null
var minimum_drag = 100
func _unhandled_input(event):
if event.is_action_pressed("ui_click"):
swipe_start = get_global_mouse_position()
if event.is_action_released("ui_click"):
_calculate_swipe(get_global_mouse_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:
print("SWIPE RIGHT")
emit_signal("swipe", "right")
else:
print("SWIPE LEFT")
emit_signal("swipe", "left")
if abs(swipe.y) > minimum_drag:
if swipe.y > 0:
print("SWIPE DOWN")
emit_signal("swipe", "down")
else:
print("SWIPE UP")
emit_signal("swipe", "up")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment