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

How do I maintain the aspect ratio of Gui objects while tweening?

Asked by
Warphi 51
4 years ago

I have a perfect square gui object that i am attempting to tween while keeping it a perfect square, how do I do this? My code is down below and is as precise as I have been able to get it to.

player.PlayerGui.ScreenGui.Frame:TweenSize(UDim2.fromScale(.05*1.10,0.17*1.34), "Out", "Linear", 2)
            player.PlayerGui.ScreenGui.Frame:TweenPosition(UDim2.fromScale(.985*.881,.985*.453), "Out", "Linear",2) 
--[[^as a side note, this is where im trying to offset it to keep the center position on the screen. if you know a good way to do this, help me out]]

1 answer

Log in to vote
0
Answered by 4 years ago

For a perfect square object, roblox has created a very useful tool which automatically keeps your GUI objects at a certain ratio. This even works with from scale, and saved me a bunch of code that I was going to make.

The object is called UIAspectRatioConstraint, and once placed inside a GUI object, has many different settings. It would be unrequired to explain thoroughly, but you can choose which axis stays dominant, and the ratio you want. When I used it, I wanted an image to be square, so I set the ratio to be 1.

To keep the object in the middle of the screen, I find the best method is simply to do the following equation for the position: UDim2.new(0.5-(ScaleX/2), -(OffsetX/2), 0.5-(ScaleY/2), -(OffsetY/2) (if you only want the X to be in the middle, only utilize the first two, and vice versa.

If you have any questions, please do reply.

Hope this has helped :)

0
Hi! Your first bit of advice was amazing, I had no idea that object existed! Thank you so much! As for the second piece, I am not trying to keep the gui in the center of the screen, but rather keep the center of the gui in the same place as the size tweening takes place. Warphi 51 — 4y
0
I will, however accept your answer because you've already helped me achieve more than I thought I was able to :) Warphi 51 — 4y
0
Ah, I understand now. That would just take some calculations, measuring the before position and after position. Glad I could help anyway. NinjaMandalorian 252 — 4y
Ad

Answer this question