Dragging kinematic bodies

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

I’ve coded a kinematic body 2D to be able to be picked up and dragged around with this code (ignore the grabbed = grabbable.GRAB)

func _process(delta):
if grabbed == grabbable.NO_GRAB:
	return
if (mouse_in && Input.is_action_pressed("left_click")):
 dragging = true
if (dragging && Input.is_action_pressed("left_click")):
	var position = get_viewport().get_mouse_position()
	set_position(position)
else:
	 dragging = false

which works perfectly fine for the dragging system i wanted, except for 3 things, one of which i sort of know how to remedy.
The first issue is that wherever i click on the screen, the body teleports to the mouse position, as opposed to only being picked up when the mouse is over the body.

The second issue is that the body does not travel with the mouse, but rather quite a bit to the side of the mouse position, with the velocity of the body being proportional to its distance from the cursor when being dragged. I initially thought this was an issue with me having the sprite and collision shape not properly lined up with the actual body, but that isnt the case as they are lined up.

The third thing is that i can currently drag the body through walls, and if released when inside a wall it just pops out. is there a way i can prevent the body from ever being allowed through walls, while still having it follow the mouse?

right ive solved the teleporting thing but still have the positioning issues

matt_day | 2020-05-11 18:44