Godot Version
4.4.1.stable.mono
Question
Problem with pong ball not bouncing but getting pushed by paddle
I am trying to create a pong/brickbreaker style game to learn a bit about how godot works, but I am stuck with a problem on recreating the expected behavior from this games.
My problem is that when the ball hits the side of the paddle, the ball does not bounce, it gets stuck to the side of the paddle and moves along side it.
This problem only occurs when the platform is moving.
Here is the code:
Ball.gd
class_name Ball
extends CharacterBody2D
const SPEED = 600.0
var direction
var target = Vector2()
func _physics_process(delta: float) -> void:
#Here so i can move the ball where i want so i can test it easier
if Input.is_action_just_pressed("left_click"):
target = get_global_mouse_position()
direction = (target - position).normalized()
velocity = direction.normalized() * SPEED * delta
#Here so i can move the ball where i want so i can test it easier
if Input.is_action_just_pressed("right_click"):
global_position = get_global_mouse_position()
var col = move_and_collide(velocity)
if(col != null):
if (col.get_collider() is Paddle):
velocity = velocity.bounce(col.get_normal())
else:
velocity = velocity.bounce(col.get_normal())
Paddle.gd
class_name Paddle
extends CharacterBody2D
const SPEED =600.0
func _physics_process(delta: float) -> void:
var direction := Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED * delta
else:
velocity.x = move_toward(velocity.x, 0, SPEED * delta)
move_and_collide(velocity)
Both are CharacterBody2ds with movement mode set to flying
Also the Collision Mask is set so that only the ball collides with the paddle, if I change the collision so that the paddle has the ball on its mask another big problem appears, the ball can actually move the paddle out of position when it hits it on certain angles
Stuck on this for the past few days and cant find the answer so any help is appreciated, thx!