Which node should I use to detect mouse or touch drag on screen in Godot

35 Views Asked by At

Which node should I use to detect mouse or touch drag on screen in Godot

enter image description here I have a UI built and now I want to detect drag to rotate the camera. Which Control node can I use to detect such an event?

1

There are 1 best solutions below

0
NewToPi On

This code appears to have worked for my project. Note: You must enable emulate touch from mouse in the project settings in order to test on the computer.

extends TextureButton

signal CameraPivot

@export var sensitivity = 0.5
var lastPosition = null

func _input(event):
    #print(event.as_text())
    if event is InputEventScreenTouch:
        #print(event.position.x)
        if event.canceled:
            lastPosition = null
        elif event.pressed && lastPosition == null:
            lastPosition = event.position.x     
        else:
            lastPosition = null
    elif event is InputEventScreenDrag && lastPosition != null:
        #print("Drag")
        var displacement:float = event.position.x - lastPosition
        var angle:float = displacement * sensitivity
        #print(displacement)
        lastPosition = event.position.x
        CameraPivot.emit(angle)
    else:
        pass
        #lastPosition = null