0 votes

I am trying to make a Camera2D object movable using the mouse, in a fashion similar to touchscreen scrolling, but I am getting an issue where the camera seems to "teleport" towards the upper left corner of the window before normal movement occurs. Is it possible to correct this behavior? So far, I am using the following code:

extends Camera2D

var isActive = false
var oldPos

func _ready():
    pass

func _input(event):
    isActive = Input.is_action_pressed("_lmb") #left mouse button

if Input.is_action_just_pressed("_lmb"):
    oldPos = get_global_mouse_position()

if event is InputEventMouseMotion:
    if isActive:
        var newPos = oldPos - event.global_position
        self.position = newPos
in Engine by (36 points)
edited by

1 Answer

0 votes
Best answer

I have found a solution to my problem. Apparently, the cursor was moving towards the upper left corner of the screen because of the fact that the origin of Godot Objects is not the center, but rather the upper left corner of the Object. So the solution to the problem is to offset that difference by adding 50% of the width and height of the control to the newPos variable like so:

var newPos = oldPos - event.global_position + (self.get_viewport_rect().size / 2)
by (36 points)
selected by
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.