Issue when use joystick and button touchscreen?

Godot Version

Godot 4.2

Question

My game has a joystick and a jump button. When I hold down the jump button and move the joystick, the joystick does not function until I release the jump button.

You may have a logic issue with your input handler.

Each finger gets an index on a first come first serve basis. I assume you have an xy area for the UI input. You need to register which finger is using that interface and it could change depending on the order of when the finger touched the screen.

#jump and move
Index 0 touches jump and holds, index one touches joystick.

#move and jump
Or index 0 touches joystick, and index 1 touches jump.

1 Like

thank you. I got help and got it resolved

extends Area2D

@onready var handle: Sprite2D = $Handle

var is_touching = false
const MAX_DISTANCE = 100
var direction:Vector2 = Vector2.ZERO
var touch_position:Vector2 = Vector2.ZERO
var touch_index:int = -1


func _process(delta: float) -> void:
	var joystick_position = global_position
	var mouse_position = touch_position
	direction = mouse_position - joystick_position
	var pos = mouse_position
	if is_touching:
		if pos.distance_to(joystick_position) > MAX_DISTANCE:
			pos = joystick_position + direction.normalized() * MAX_DISTANCE
		handle.global_position = pos
	else:
		handle.global_position = joystick_position
		direction = Vector2.ZERO

func _unhandled_input(event: InputEvent) -> void:
	if is_touching:
		if event is InputEventScreenDrag and event.index == touch_index:
			touch_position = event.position # Update the touch position
			get_viewport().set_input_as_handled() # Handle the input, don't propagate it
		if event is InputEventScreenTouch and event.index == touch_index:
			is_touching = false # Touch was released
			touch_position = event.position # Save the touch position
			touch_index = -1 # Reset the touch index
			get_viewport().set_input_as_handled() # Handle the input, don't propagate it


func _input_event(viewport: Viewport, event: InputEvent, shape_idx: int) -> void:
	if not is_touching and event is InputEventScreenTouch:
		is_touching = true # a touch event entered the collision shape
		touch_position = event.position # Save the touch position
		touch_index = event.index # Save the index which triggered the touch event


func get_direction() -> Vector2:
	return direction.normalized()
1 Like