I am totally new to game developing and I am just trying to learn. I am sorry if I lack some terms, but I will try to explain what I want.
I was trying to figure out how to pick up an object and move it along with the mouse (drag and drop logic)
I found a code online that helped me with this, but it sets the position of the object (KinematicBody2D) to "getviewport().getmouse_position()" to make it move along with the mouse. I want to maintain the distance and the direction between the object and the mouse. For example, If I pressed and hold on the left corner of the object and dragged up the object will move in a parallel line.

My code looks like this:
func _process(delta):
if (mouse_in && Input.is_action_pressed("left_mouse_click")):
dragging = true
if (dragging && Input.is_action_pressed("left_mouse_click")):
var newPosition = Vector2()
if(position.x < get_viewport().get_mouse_position().x):
newPosition.x = get_viewport().get_mouse_position().x - x
else:
newPosition.x = get_viewport().get_mouse_position().x + x
if(position.y < get_viewport().get_mouse_position().y):
newPosition.y = get_viewport().get_mouse_position().y - x
else:
newPosition.y = get_viewport().get_mouse_position().y + x
position = newPosition
set_position(position)
else:
dragging = false
-X is suppose to be the fixed distance between the initial mouse click and the object position. I didn't know how to obtain that so I use an int 16.
-mouse_in is set to true in mouse entered event and false in mouse existed.
This code seems to be doing what I want but if I move the mouse slowly and if I go fast the position of the object start changing to the right or left of the mouse position.
I would appreciate the help
Thank you