# How do I fix collision errors at the edge of my paddle?

Probably a last resort to see what’s actually going on, here’s the entire script:

``````extends CharacterBody2D

@export var speed = 600
var score = 0
var speed_increase = 10

visible = true
var direction = 1
direction = randi_range(1, 4)
if direction == 1:
velocity = Vector2.RIGHT.rotated(PI/4)
elif direction == 2:
velocity = Vector2.LEFT.rotated(PI/4)
elif direction == 3:
velocity = Vector2.LEFT.rotated(PI/-5)
elif direction == 4:
velocity = Vector2.RIGHT.rotated(PI/-5)

func _physics_process(delta: float) -> void:
var collision = move_and_collide(velocity * speed * delta)
if not collision:
return

if collision.get_normal() != Vector2.UP:
velocity = velocity.bounce(collision.get_normal()) * speed
if collision.get_collider().name == "Paddle" and %Timer.is_stopped():
score += 1
label.text = str(score)
%Timer.start()
speed += 10
print(speed)
hit_sound_2.play()
hit_sound.play()
if collision.get_collider().name == "Borders":
hit_sound.play()

if collision.get_collider().name == "DeathArea":
pop_sound.play()
game_over_sound.play()
visible = false
game_over_screen.visible = true
get_tree().paused = true

if collision.get_collider().name == "Paddle" or "Borders" and %Dust.emitting == true:
Spawn_Dust_2(collision.get_position())
else:
Spawn_Dust(collision.get_position())

velocity = velocity.bounce(collision.get_normal())

func _on_timer_timeout():
%Timer.stop()

func _on_play_again_pressed():
print("Restarted")
game_over_screen.visible = false
get_tree().paused = false
get_tree().change_scene_to_file("res://Scenes/Game.tscn")

game_over_screen.visible = false
get_tree().paused = false

func Spawn_Dust(collision_point: Vector2) -> void:
%Dust.global_position = collision_point
%Dust.emitting = true
%Dust.restart()

func Spawn_Dust_2(collision_point: Vector2) -> void:
%Dust2.global_position = collision_point
%Dust2.emitting = true
%Dust2.restart()

``````

What’s wrong with my code here?

Where else should I apply delta though?

If it’s never moving I would double check speed and velocity are not zero, maybe a print on ready `print("velocity: ", velocity, " and speed: ", speed)`

Everything looks fine

But when I try to print a function, it doesn’t do anything or give any input to the console. Like it never even started in the first place

Huh, did your ball script get detached? You can check in the inspector for Ball, very bottom will list what script is attached or hovering over the script icon if it is present

Oh, I just checked and that’s actually what happened. I’ll fix it

When the ball hits the edge of the paddle, it goes HYPER (which actually scared me, lol) so I may have to look into that. is there any problem in my script though?

That part is the bounce multiplied by speed, it looks like you actually have `velocity.bounce` twice in your script so I believe you can remove this section entirely.

``````	if collision.get_collider().name == "Paddle":
if collision.get_normal() != Vector2.UP:
velocity = velocity.bounce(collision.get_normal()) * speed
``````

or if you want it to speed up on the side of the paddle, try adding to speed instead of bouncing

Dont worry, I’ve fixed it. just had to remove the * speed after collision.get_normal thing, but now the ball sometimes stays inside the paddle and doesn’t move out.

Just read your post now, And I’ll prob try removing the section and see if it works

No difference, the ball just goes stays on the edge and doesn’t come out

Is the paddle using `move_and_slide`/`move_and_collide` or changing position directly? If it’s stuck inside the paddle I could see that being the issue. If it’s stuck outside the paddle then i’m not sure, sounds like double bounce or skipping bounce for some reason

It’s stuck outside, I’ll check my script for any double bouncing then

I think it’s this script here:
if collision.get_collider().name == “Paddle” or “Borders” and %Dust.emitting == true:
Spawn_Dust_2(collision.get_position())
else:
Spawn_Dust(collision.get_position())

``````velocity = velocity.bounce(collision.get_normal())
``````

That’s the only bounce I would keep, it applies to all hits.

The if statement is funky, I went over how I would change it, but it won’t be related to getting stuck.

The way my code is structured has different sound effects depending on which one was collided with. I’ll check if there any more bounces in the code and remove them.

There’s no other Bounce function in my script apart from the one just mentioned

1 Like

Is there anything I’m doing wrong with the script?

I can’t tell, it’s only when bouncing on the side of the paddle? is there a difference if the paddle is moving or is stationary when the ball hits?