0 votes

I am basically making pong. The ball doesn't bounce back after hitting the paddles or the level bounds when I use moveandslide(). I am a beginner so someone please help. lol

in Engine by (44 points)

1 Answer

0 votes

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)
by (178 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.