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

How do I make a GUI tween from the center of the GUI?

Asked by
alibix123 175
8 years ago

I have a GUI menu that changes the size of a text button when the mouse hovers over the button. This is my code to resize the button:

button:TweenSize(UDim2.new(0.65, 0, 0.12, 0), "Out", "Quad", 0.01)

The dimensions are quite specific but hopefully this gif shows it better: http://imgur.com/Nml4gPO

When the mouse hovers over the button, the size is tweened from the top left. How do I make it so that the GUI tweens outwards from the center equally in all directions?

0
Use TweenSizeAndPosition, setting the Position being moving the GUI to the center while to the user is looks like it's expanding from the center. M39a9am3R 3210 — 8y
0
So would I do that? Is there like a formula for getting the center? alibix123 175 — 8y
0
So how would I* alibix123 175 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

TweenSizeAndPosition

It's simple really. If you're increasing the size, change the position at the same time (By about half the amount you're increasing the size by).

Try this utility function for it.

function TweenFromCentre(obj, newdim, ...)
    local os, op = obj.Size, obj.Position;
    local dsx,dsy,dox,doy = newdim.Scale.X - os.Scale.X, newdim.Scale.Y - os.Scale.Y, newdim.Offset.X - os.Offset.X, newdim.Offset.Y - os.Offset.Y
    return obj:TweenSizeAndPosition(
        newdim, 
        UDim2.new(
            op.Offset.X - dox*.5,
            op.Scale.X - dsx*.5,
            op.Offset.Y - doy*.5,
            op.Scale.Y - dsy*.5
        ),
        ...
    );
end;

How does it work?

It changes the size, and gets the difference in size from the start of the tween to the end of the tween. Then it makes sure to move the position of the thing by half the difference to offset the change. The result is that the object changes size from the centre.

Ad

Answer this question