How to follow touch screeen pressed/drag position instead of get_global_mouse_position()?

Godot Version



The player is constantly following the mouse position.

class_name Player extends Area2D

func _physics_process(delta):
	if is_playing:
		global_position = global_position.lerp(get_global_mouse_position(), delta * movement_speed)

This works with mobile too, but… If you for example tap level 1, when level 1 starts, the player moves to the last position tapped before the level started.

This was easy to solve on computer with get_viewport().warp_mouse() but that doesn’t work with mobile.

This is a mobile game, so the mouse should be secondary, rather than the way it is now.
How can I make the player follow the touch position instead?
How do you even get the global touch position?

In input function do that:

func _input(event):
   if event is InputEventScreenTouch and event.is_pressed():
      sprite.position = event.position #or get_global_mouse_position()

That will only work when clicking once and even if it was taking a drag event into account, it is not taking movement speed into account but would be instantaneous.

So what do you want?

The equivalent of this fictive example:

if is_playing and touch_device_is_being_pressed:
  global_position = global_position.lerp(get_global_touch_pressed_position(), delta * movement_speed)

OR the equivalent of this fictive example:

var spawn_position = Vector2(540, 1540)


I solved it like this for now:

I have an autoload / singleton called G (, in which I have among other global methods, constants and variables:

func _input(event):
	if event is InputEventScreenTouch:
		if event.is_pressed():
			touching += 1
			touching = max(0, touching - 1) # the screen could already be pressed when the level starts

Then I do:

if is_playing and G.touching == 0: # because of multi-touch