+2 votes

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.

in Engine by (53 points)

Did you find a solution in the meantime?

1 Answer

0 votes
Best answer

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()
by (53 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.