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

How can you keep an object centered while using :TweenSize()?

Asked by
wackem 50
8 years ago

So, everything works fine, but I don't know how to go around keeping the object centered where it was previously.

Code:

local b = script.Parent
local wait = 5

b.MouseButton1Down:connect(function()
    b:TweenSize(UDim2.new(0, 150, 0, 150), nil, "Linear", wait, nil)
end)

1 answer

Log in to vote
0
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