How do I drop it to where I touched it?

Godot Version

4

Question

Hi. I’m making a suika game like the one in the image while watching youtube. Right now I’m moving it left and right with the A and D keys and dropping it where I want it with a mouse click. But I want to drop it to the desired location with just a touch of the screen with my finger, without the left and right key movement. how can I do that?

Hey there,

for touchscreen you could use this :

func _input(event):
    if event.is_pressed():
        $Object.position.x = event.position.x

1 Like

Not limited to touchscreens. Also, it’s good practice to check the event’s type (or it will also trigger for e.g. pressed keys on the user’s keyboard):

func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		$Object.position.x = event.position.x

For touch input, the correct type would be InputEventScreenTouch.

I’m following a youtube tutorial and the left and right movement is done with the keyboard. I want to be able to move by touch like in the example below. How do I implement this so that when I touch the location I want to drop, the object moves horizontally to that location and then drops? Where do I modify or add in the GD script below?

[script_main]
func _process(delta):
$SelectMash.position = $Player.position
if Input.is_action_just_pressed(“drop”,true):
if $SelectMash.visible:
if !$GameOver.visible:
drop()
await get_tree().create_timer(1.0).timeout
change_image()

func drop():
var dropmash = mash_scene[nowIndex].instantiate()
add_child(dropmash)
dropmash.position = $SelectMash.position
$SelectMash.hide()

[script_player]
func _process(delta):
var input := Vector2.ZERO
input.x = Input.get_axis(“move_left”,“move_right”)

position.x += input.x * speed