For a prototype I wanted to implement mobile swipe controls to move my player on the horizontal axis.
The first thing to make sure is to check the setting under “Project Settings -> General -> Input Devices -> Pointing”. The settings “Emulate Mouse From Touch” needs to be active.
In your node which shall be controlled by the swipe control you need the following code:
extends CharacterBody2D
var swipe_start : Vector2
var processing_swipe : bool = false
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventScreenTouch:
if event.is_pressed():
if not processing_swipe:
processing_swipe = true
swipe_start = event.position
else:
processing_swipe = false
elif event is InputEventScreenDrag:
calculate_swipe(event.position)
func calculate_swipe(position_now : Vector2) -> void:
var diff = swipe_start - position_now
global_position.x -= diff.x
swipe_start = position_now
I created this code with Godot 4.6.
