Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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 :)

3 comments

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.

0

I will, however accept your answer because you've already helped me achieve more than I thought I was able to :)

0

Ah, I understand now. That would just take some calculations, measuring the before position and after position. Glad I could help anyway.

This discussion is preserved as it was. New answers, comments, and votes are closed.