The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

Whats the best way to create a paddle for a Air Hockey Game?
I'm using a rigid body and here's my current code. But it looks really ugly when playing, because it moves only after the distance to player is bigger than 20 px. And if I remove that distance check, its starts to bounce on that point, like jittering.

extends RigidBody2D

const SPEED = 2000

var destination = null

func _integrate_forces(_state):
    if destination != null:
        var direction = (destination - global_position).normalized()
        var distance_to_player = global_position.distance_to(destination)
        if distance_to_player > 20:
            linear_velocity = direction * SPEED
        elif distance_to_player > 3:
            linear_velocity = direction * SPEED * 0.20
        else:
            linear_velocity = Vector2.ZERO
in Engine by (44 points)

1 Answer

0 votes

I found the solution for now by multiplying the distance to touch position to the linear velocity.extends RigidBody2D

export var touch_rect:Rect2

const SPEED = 30

var destination = null

func _integrate_forces(_state):
    if destination != null:
        var direction = (destination - global_position).normalized()
        var distance_to_player = global_position.distance_to(destination)
        linear_velocity = direction * SPEED * distance_to_player


func _input(event):
    if touch_rect.has_point(event.position):
        destination = event.position
by (44 points)

This "solution" has one tiny drawback though: the paddle will move with different speeds depending on the distance towards the destination. See my answer over here for a solution without that problem (it's for a KinematicBody2D though).

This is interesting. I think I can learn from this.

Yes you're right. Thanks for your solution!
I'll try your solution too, maybe it's perfect to create the CPU, because with my movement the CPU doesn't play well because if the speed problem you mentioned.

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 Frequently asked questions and 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 [email protected] with your username.