Im making a pong game and while Ive mostly succeeded at getting the ball to work correctly It has problems with the paddle. Specifically, when the paddle hits the ball it acts more like a moving wall and it usually ends up looking more like its pushing the ball rather than launching it. In my game the paddles can freely move and rotate. What can I do to improve this? The code for the ball is this:
extends CharacterBody2D
@onready var speed_timer: Timer = $speed_timer
var screen_size: Vector2
var dir = get_ran_dir()
var speed = 100
var out_bounds: bool = false
var middle = Vector2()
var high: bool
var low: bool
func _ready() -> void:
screen_size = get_viewport_rect().size
position = Vector2(screen_size.x / 2, screen_size.y / 2)
set_multiplayer_authority(multiplayer.get_unique_id())
func _physics_process(delta: float) -> void:
var collision = move_and_collide(dir * speed * delta)
if collision:
speed *= 1.2
dir = dir.bounce(collision.get_normal())
position_tracking()
func get_ran_dir() -> Vector2:
var new_dir = Vector2()
new_dir.x = [1,-1].pick_random()
new_dir.y = [1,-1].pick_random()
return new_dir.normalized()
func position_tracking() -> void:
high = position.x >= screen_size.x / 1.01 or position.y >= screen_size.y
low = position.x <= -screen_size.x / 1.01 or position.y <= -screen_size.y
middle = Vector2(screen_size.x / 2, screen_size.y / 2)
screen_size = get_viewport_rect().size
if high or low:
position = middle
speed = 100
print("return")
and for the paddle its this:
extends CharacterBody2D
@onready var charge_time: Timer = $charge_time
@onready var ball
const SPEED = 300.0
@export var rotation_speed = 3.0 # Speed of rotation
@export var speed = 400.0 # Movement speed
func _ready() -> void:
ball = get_parent().get_node("Ball")
func _physics_process(delta: float) -> void:
if !is_multiplayer_authority(): return
var direction := Input.get_axis("Left", "Right")
var direction_hor := Input.get_axis("up", "down")
velocity.x = direction * SPEED if direction else move_toward(velocity.x, 0, SPEED)
velocity.y = direction_hor * SPEED if direction_hor else move_toward(velocity.y, 0, SPEED)
move_and_slide()
if Input.is_action_pressed("Rotate clockwise"):
rotation -= rotation_speed * delta
elif Input.is_action_pressed("Rotate counter clockwise"):
rotation += rotation_speed * delta
var collision = move_and_collide(velocity * delta)
if collision:
var colider = collision.get_collider()
if colider == ball:
ball.speed += 1000
Another problem is that for some reason upon colliding the ball sometimes drags the paddle with it after bouncing.