Android SCREEN_DRAG to pan camera

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


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)

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:

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():
func _input(event):
    if event is InputEventScreenTouch:
        if event.pressed:

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