How can I make an moving pong ball

I am trying to make an moving ball however its hard.

Here is my code:

local briques = createGameBoard()

while true do
  if ball.Position.Z > gameBoard.bounds["left bound"].Position.Z then
    ball.Position += Vector3.new(1, 0, 0)
  elseif ball.Position.Z > gameBoard.bounds["right bound"].Position.Z then
    ball.Position -= Vector3.new(1, 0, 0)
  end
 
  wait(0.1)
end

It’s supposed to bounce the ball once its reaching one of the boundaries wall and when it touche the paddle(raquette) and when it touches an brick

You might be able to make a .Touched event on some borders instead of using a forever loop. This may make it easier and much more so optimized.

How would you wait for multiple .Touched event for the ball

Can you explain the game to me that you are making because there might be other solutions for this.

I am making an Atari breakout clone. It generate randomly multiple brick of the same size.

Is it clear enough or I need to add more detail

Then you may be able to use Magnitude to find the distance between the objects as well, The ball’s position - The outer bounds, correct? and you may want to use TweenService instead of just moving the ball instantly If i’m thinking of the game that you are going for accurately.

1 Like
local wallTouched
gameBoard.bounds["left bound"].Touched:Connect(function () 
  wallTouched = "left"
end)
gameBoard.bounds["right bound"].Touched:Connect(function () 
  wallTouched = "right"
end)
gameBoard.bounds["top bound"].Touched:Connect(function () 
  wallTouched = "top"
end)
gameBoard.bounds["bottom bound"].Touched:Connect(function () 
  wallTouched = "bottom"
end)

while true do
  if wallTouched then
    if wallTouched == "left" then
      ball.Position -= Vector3.new(0, 0, 0.1)
    elseif wallTouched == "right" then
      ball.Position += Vector3.new(0, 0, 0.1)
    elseif wallTouched == "top" then
      ball.Position -= Vector3.new(0, 0.1, 0)
    elseif wallTouched == "bottom" then
      ball.Position += Vector3.new(0, 0.1, 0)
    end
  end
  wait(0.1)
end

I am thinking of making something like this. is it good in your opinion

Above ^^

Also whenever it would hit the borders wouldn’t it go down/up there would be some math included to find out which direction it needs to travel if it’s coming from below and going up or going from above and going down when it hit a brick and then hits the wall.

The problem with the .Touched Solution is that it require the part to be colliding with other parts. To collide with other part it require the ball to be welded or anchored to other parts.

can you make like art to show me what do you want to make


The red thing on the side are the wall.

you are basically asking how do i make a very simple physics engine. just have the ball move every frame based on a velocity and have the velocity change upon collision, reflecting it across the surface it touched

1 Like