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

Positioning a GUI button using scale and mouse position?

Asked by
SuperPuiu 497 Moderation Voter
2 years ago

Hello!

Currently I'm working on a game that will use only buttons. You will be able to place a button at the mouse position and I want it to be in the same position for every device.

I think it is possible, but I didn't find the right idea. Any suggestion on how to do that? Currently, I'm trying to set the button's position to UDim2.new(0.5, Mouse.X / 2, 0.5, Mouse.Y / 2), which gives a weird result.

1 answer

Log in to vote
0
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
UDim2.fromScale(Mouse.X / ScreenGui.AbsoluteSize.X, (Mouse.Y - 36) / ScreenGui.AbsoluteSize.Y)

I think something like this will work since Mouse.X is a number between 0 and screen width, ScreenGui.AbsoluteSize.X is the actualy screen width, so dividing Mouse.X by this number will always return you a number from 0 to 1, that is the Scale position that you need. I subtract 36 from Mouse.Y since there is the topbar offset added automatically, not sure if that's an issue for you. You should also subtract Mouse.X with half of the button's offset Size to make it centered.

Ad

Answer this question