Android SCREEN_DRAG to pan camera

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Coenster

Hello

I’m still using Godot 2.1.4 because of GLES2 not being in GODOT3

I want to drag across the screen to pan the camera and limit the camera not to pan to far. This is what I’ve got:

 func pan_camera(val):
     var p = get_node("Camera").get_pos()
     var pan = current_pan_move * pan_rate
     p.x = clamp(p.x + pan, -150, 150)
   	 get_node("Camera").set_pos(p)

This obviously only pan the x axis, and no matter what I add, i can’t pan the x and y axis at the same time. I did use this code to pan and use the camera limit to prevent the camera from scrolling too far:

if event.type == InputEvent.MOUSE_MOTION:
	move_local_x(-event.relative_x)
	move_local_y(-event.relative_y)

But this was a nightmare, it messed with my zoom(didn’t zoom to the center of my scene anymore, couldn’t fix it) and the camera got stuck whenever it reached the pan limit.

How can my first code be edited to allow the camera to pan on x and y axis?

If you have better way to pan the camera with SCREEN_DRAG and I will appreciate it.

Did you find a solution in the meantime?

TobiLa | 2018-07-20 09:08

:bust_in_silhouette: Reply From: Coenster

I eventually moved to godot 3.1 and was able to get the desired effect with the following code and setting the limits for the camera in the inspector:

var touch_vector = Vector2()
var primary_touch = false

func _ready():
    set_process_input(true)
    
func _input(event):
    if event is InputEventScreenTouch:
        if event.pressed:
            pass

    if event is InputEventScreenDrag:
	if not primary_touch:
		touch_vector = event.get_relative()
		primary_touch = true
	else:
		var result = event.get_relative()*3 - touch_vector
		set_position(get_position() + result)
		primary_touch = false
	get_tree().set_input_as_handled()