Werid Gui Offset


I’m trying to create a joystick type thing and ran into this weird offset error. I’m not sure what caused this and what attempts I can make to try to fix it.

Code
local radius = self.JoyStick.Parent.Size.Offset.X / 2
local pos = self.JoyStick.Position
self.JoyStick.Position = UDim2.new(
    .5,
    math.clamp(pos.X.Offset, 0, radius),
    .5,
    math.clamp(pos.Y.Offset,0, radius)
)

It would mean a ton if you could help, thanks!

Are you trying to make it fit every device screen?

A UDim2 value holds 4 coordinate positions (scale and offset) used for 2D coordinating.

since you’re initially setting the scale of each position to 0.5, the joystick position will start at (0.5, 0.5) on the x-y axis, and then add the additional math.clamp values to the offset as shown.

you would need to clamp a negative version of the radius AND a positive version of the radius. for example:

self.JoyStick.Position = UDim2.new(
    .5,
    math.clamp(pos.X.Offset, -(radius), radius),
    .5,
    math.clamp(pos.Y.Offset,-(radius), radius)
)

however, i am not completely sure.

give it a shot and let me know what you come across!

1 Like

Go to the GUI and go to size. Then open the X and Y. After that change both offsets to 0. Then you can mess with the Y scaling. Setting the offset to 0 will make it fit any device. Than when you scale it with the Y scale it should fit anywhere you want it. Here is an example I used in my game:
image