luaroblox2d-gamespong

Roblox PONG Collision And Direction Not Working


I'm making a PONG game in Roblox, but it seems the ball keeps going to The bottom left, and sometimes the bottom right of the screen, the problem with this - is that its supposed to fly left or right, and the collision with the bars and the ball doesn't work, and all this is running in a frame, but even the frame doesn't have collision with the ball.

local UserInputService = game:GetService("UserInputService")

local ball = script.Parent.ball
local frame1 = script.Parent.Frame1
local frame2 = script.Parent.Frame2

local initialDirection = math.random(0, 1) == 0 and -0.01 or 0.01
local ballSpeed = Vector2.new(initialDirection, 0.01)

local function moveFrames(direction)
    local newPosition
    if direction == "up" then
        newPosition = UDim2.new(0, 0, -0.1, 0)
    elseif direction == "down" then
        newPosition = UDim2.new(0, 0, 0.1, 0)
    end

    if frame1.Position.Y.Scale + newPosition.Y.Scale >= 0 and frame1.Position.Y.Scale + newPosition.Y.Scale + frame1.Size.Y.Scale <= 1 then
        frame1.Position = frame1.Position + newPosition
    end

    if frame2.Position.Y.Scale + newPosition.Y.Scale >= 0 and frame2.Position.Y.Scale + newPosition.Y.Scale + frame2.Size.Y.Scale <= 1 then
        frame2.Position = frame2.Position + newPosition
    end
end

local function onInputBegan(input, gameProcessed)
    if gameProcessed then return end

    if input.KeyCode == Enum.KeyCode.W then
        moveFrames("up")
    elseif input.KeyCode == Enum.KeyCode.S then
        moveFrames("down")
    end
end

local function updateBallPosition()
    ball.Position = ball.Position + UDim2.new(ballSpeed.X, 0, ballSpeed.Y, 0)
end

local function checkBallCollision()
    if ball.Position.Y.Scale <= 0 or ball.Position.Y.Scale + ball.Size.Y.Scale >= 1 then
        ballSpeed = Vector2.new(ballSpeed.X, -ballSpeed.Y)
    end

    if ball.Position.X.Scale <= frame1.Position.X.Scale + frame1.Size.X.Scale and
        ball.Position.Y.Scale + ball.Size.Y.Scale >= frame1.Position.Y.Scale and
        ball.Position.Y.Scale <= frame1.Position.Y.Scale + frame1.Size.Y.Scale then
        ballSpeed = Vector2.new(-ballSpeed.X, ballSpeed.Y)
    end

    if ball.Position.X.Scale + ball.Size.X.Scale >= frame2.Position.X.Scale and
        ball.Position.Y.Scale + ball.Size.Y.Scale >= frame2.Position.Y.Scale and
        ball.Position.Y.Scale <= frame2.Position.Y.Scale + frame2.Size.Y.Scale then
        ballSpeed = Vector2.new(-ballSpeed.X, ballSpeed.Y)
    end
end

local function pongGameLoop()
    while true do
        checkBallCollision()
        updateBallPosition()
        wait(0.03)
    end
end

UserInputService.InputBegan:Connect(onInputBegan)
pongGameLoop()

Nothing really worked, maybe someone can fix it.


Solution

  • Ball goes to bottom left or bottom right: it happens because in your ballSpeed variable, speed by Y axis is 0.01, so to make ball fly left or right: you need to change Y axis to 0:

    local ballSpeed = Vector2.new(initialDirection, 0)

    Collision with the bars and the ball doesn't work: it works fine for me. I think that it does't work for you, because you made size of Frame1, Frame2 and ball by Offset. If you do, change size of these objects to Scale or change your code.

    You can also see game where I tested your code (it expires in 7 days): https://fex.net/s/a5bpt5d