0 votes

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:

enter image description here

Does anyone have an idea where the problem is?

in Engine by (16 points)

1 Answer

0 votes

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)
by (16 points)
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.