Topic was automatically imported from the old Question2Answer platform.
Asked By
CynicalKazu
I am basically making pong. The ball doesn’t bounce back after hitting the paddles or the level bounds when I use move_and_slide(). I am a beginner so someone please help. lol
i use to move the ball a direction (vector2()) and a speed (int or float), then in my process i get the velocity thus:
func _process(delta: float) -> void:
# Get velocity
var velocity = speed * delta * direction # move slow? increases speed or multiply this x 100
# move and slide:
move_and_slide(velocity)
# check if there is a collision:
if get_slide_count() > 0:
var collision = get_slide_collision(0)
if collision != null:
direction = direction.bounce(collision.normal) # do ball bounce
You can create your variable direction with random values for x and y from values -1 to 1. then when ball get a collision, it bounce in right direction
var direction = Vector2()
var speed = 500
func _ready():
randomize()
# start with random direction beetween -1 and 1
direction.x = rand_range(-1, 1)
direction.y = rand_range(-1, 1)