Script to move the Camera2d in godot v2.1

Luto-AkinoLuto-Akino Posts: 7Member

I see that many people are having problems with moving the 2d camera with the mouse/touch, I was one of them, so I leave you a script that I made for godot v2.1 that I hope will facilitate you the life and development of your games. :)

(remember to activate the touchscreen emulation option in project settings and create "mb_left" in the input map options and set the option fixed topleft in the anchor mode option of the camera2d) ;)

Good Luck!! :)

extends Node2D
var cam
var pretouch = Vector2()
var dragging = false
func _ready():
    cam = get_node("Camera2D")
    set_process_input(true)
    pass
func _input(event):
    if event.is_action_pressed("mb_left"):
        pretouch = Vector2(get_global_mouse_pos().x,get_global_mouse_pos().y)
    if event.type == InputEvent.SCREEN_DRAG:
        var current_touch = Vector2(event.x,event.y)
        var v2d = pretouch - current_touch
        if (v2d != Vector2(0,0)):
            dragging = true
            cam.set_global_pos(v2d)
    if event.is_action_released("mb_left"):
        dragging = false

Tags :

Comments

  • CalinouCalinou Posts: 217Admin Godot Developer
    edited February 8

    Note that you can remove the pass keyword, since the func _ready(): block already contains code (you only need pass if a block doesn't contain any code yet).

  • Luto-AkinoLuto-Akino Posts: 7Member

    @Calinou said:
    Note that you can remove the pass keyword, since the func _ready(): block already contains code (you only need pass if a block doesn't contain any code yet).

    oh i got it, thanks! :)

Sign In or Register to comment.