Making a Breakout clone, paddle moves when ball hits the side/corner

Godot Version

4.4 Beta 1

Question

I’m using a capsule collision body on the paddle, and circle on the ball. Both are CharacterBody2Ds. Both are set to be “Floating”. The code is:

Ball:

extends CharacterBody2D

@export var speed = 400.0

func _ready() -> void:
	set_velocity(Vector2(-speed, speed))
	
func _physics_process(delta: float) -> void:
	var collision = move_and_collide(velocity * delta)
	if collision:
		velocity = velocity.bounce(collision.get_normal())

Paddle:

extends CharacterBody2D

@export var speed = 500

func _physics_process(delta: float) -> void:
	velocity = Vector2(0,0)
	
	var direction = Input.get_axis("move_left", "move_right")
	
	if direction:
		velocity.x = direction * speed
		
	move_and_slide()

I’ve tried adding velocity.y = 0 above move_and_slide() and the paddle still moves on the y axis when hit on the side/corner.

What am I doing wrong here? Why does the paddle move up / down (Maybe 10-15px) when the ball hits a corner or the side of the paddle? How can I prevent this? The paddle doesn’t move when it gets hit on the top

It’s most likely because it’s sliding on the ball.
Try using move_and_collide instead of the move_and_slide.

1 Like