+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

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.