Alternative to accelerometer input - Godot Mobile Course

Hi guys,

For anyone interested in an alternative input system without the use of on-screen buttons, I will leave instructions down below.
I am using the left half of the screen to move left and the right half to move right by detecting screen touch.
Don’t forget to enable the option “Enable touch from mouse” in the project settings so you cant test on the PC.

  • Make the variable “direction” a global variable in the “player” script.
  • Then add this code to the “player” script:
func _input(event):
	if event is InputEventScreenTouch:
		if event.is_pressed():
			if event.position.x > viewport_size.x / 2:
				direction = 1
			elif event.position.x < viewport_size.x / 2:
				direction = -1
		else:
			direction = 0
  • Then remove/uncomment the following line of code from the “player” script inside the “physics_process” function:

var direction = Input.get_axis("move_left", "move_right")

and that’s it, enjoy.

Privacy & Terms