How to drag the player yet lock him on X axis?

Godot Version

4.2.2.stable

Question

Hello, me and my friend are making a mobile game about dodging blocks on the beat (hence its name is BeatBlock)

For my game, I need to be able for the user to drag the player but the player should stay locked on the X axis as to avoid the incoming blocks. But how could I implement that ? Right now I have got some basic code that moves the player when TAPPING and not by dragging.

How could I update this code so instead the player is always aligned with the users finger but locked on the X axis ? Thanks in advance.

It would be good to familiarise with the other InputEvent types here:

You will find directly below InputEventScreenTouch is InputEventScreenDrag which should suffice.

As for locking the player to the x axis, just comment out line 24?
edit: and remove the inline if statement. Not sure what purpose this serves. Surely it’s just:

func _input(event):
	if event is InputEventScreenDrag:
		position.x = event.position.x

Thank you very much ! I didn’t really think about that :slight_smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.