Moving with mouse + test_move Method

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

Hello!

I have a question regarding the mouse-movement. I’m new to Godot and trying to make a breakout clone but I’m stuck. Here is the code I use to move the paddle:

func process_mouse():
    var target = get_viewport().get_mouse_position().x
	if position.x < target:
		if not test_move(Transform2D(transform), Vector2(1, 0)):
			position.x = target
	elif position.x > target:
		if not test_move(Transform2D(transform), Vector2(-1, 0)):
			position.x = target

It collides with the surrounding collision box as long as you move the mouse “normal”; if you move the mouse pretty fast the paddle moves inside the collision box and gets stuck like this:

Does anyone have an idea where the problem is?

:bust_in_silhouette: Reply From: expensne

Nevermind, fixed it with this code:

func process_mouse():
	var target = get_viewport().get_mouse_position().x
	var paddle_width = get_node("CollisionShape2D").shape.extents.x
	var viewport_width = get_viewport().size.x
	if position.x < target:
		if not test_move(Transform2D(transform), Vector2(1, 0)):
			position.x = min(target, viewport_width - paddle_width)
	elif position.x > target:
		if not test_move(Transform2D(transform), Vector2(-1, 0)):
			position.x = max(target, paddle_width)