Buggy Collisions With Draggable Characters

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

I have a character in my game that I can drag around. I do this using this simple snippet of code:

func _process(delta):
	if $TouchScreenButton.is_pressed():
		position = get_global_mouse_position()

(the button is on top of the object relative to the camera)

However, this causes the KinematicBody2D to override most of its collisions, especially the fast ones, and simply glitch into everything because that’s where the mouse is.

Does anyone know how I could fix this?


:bust_in_silhouette: Reply From: Lopy

Instead of setting the position directly, you could use move_and_collide or move_and_slide. The vector by which to move is get_global_mouse_position() - position.

Physics related functions should be called inside a _physics_process() (or functions called inside one) to avoid issues.

You can access documentation on classes or functions by control clicking on their name inside code.