Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I work with Gui Collision?

Asked by 10 years ago

A user controls a frame with arrow keys and moves it around, I don't know where to start when making it so if the frame called Pixel touches other frames called Border or Block it will not allow them to move past that frame or move into it. How would I completely disable movement in the direction they are going when the pixel frame touches another frame?

How would I use Gui Collision? How do I use it? What methods should I use?

I just want a better understanding and what I need to do to apply it with what I am doing.

1 answer

Log in to vote
1
Answered by 10 years ago

Here's a function to check for GUI collision. You call it like this:

if isColliding(enemy, player) then --blah end

function isColliding(body1, body2)
    local b1x = body1.Position.X.Scale + body1.Position.X.Offset
    local b1y = body1.Position.Y.Scale + body1.Position.Y.Offset
    local b1sx = body1.Size.X.Scale + body1.Size.X.Offset
    local b1sy = body1.Size.Y.Scale + body1.Size.Y.Offset

    local b2x = body2.Position.X.Scale + body2.Position.X.Offset
    local b2y = body2.Position.Y.Scale + body2.Position.Y.Offset
    local b2sx = body2.Size.X.Scale + body2.Size.X.Offset
    local b2sy = body2.Size.Y.Scale + body2.Size.Y.Offset

    return (
        b1x + b1sx > b2x and
        b1x < b2x + b2sx and
        b1y + b1sy > b2y and
        b1y < b2y + b2sy
    )
end

GUI collision is actually really simple once you wrap your head around it. It's just simple comparisons using > and <. If you want an in-depth explanation, just ask.

0
I sent you a message on roblox. IntellectualBeing 430 — 10y
Ad

Answer this question