I am trying to make a button follow the mouse but it seems to be be very inaccurate
local delta = UserInputService:GetMouseLocation()
local pos = UDim2.new(0, delta.X-item.AbsolutePosition.X, 0, delta.Y-item.AbsolutePosition.Y)
item.Position = pos
local delta = UserInputService:GetMouseLocation()
local pos = UDim2.new(0, delta.X-item.AbsolutePosition.X, 0, delta.Y-item.AbsolutePosition.Y)
item.Position = pos
Delta should only be calculated once when you start the click. Where delta is the difference between the mouse and the absolute position of the GUI item.
Then for the per frame update you would just set the gui item position to mousePos + delta. (Or - delta depending on how you calculated it)
ok I will try that (ignore.ad)
local delta = UserInputService:GetMouseLocation()
ChangedConnection = UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
if Holding == true then
local pos = UDim2.new(0, delta.X-item.AbsolutePosition.X, 0, delta.Y-item.AbsolutePosition.Y)
--TweenService:Create(item, TweenInfo.new(.2), {Position = UDim2.new(0, delta.X, 0, delta.Y)}):Play()
item.Position = pos
end
end
end)
Unless you mean to center the gui which on reread might be more likely. In which case your code works, but you should be using absolute size/2 instead of absolute position
Nearly. Delta needs to be mousePos - Vector2(item.Pos.X, item.Pos.Y)
And the actual item.Position needs to be mousePos.X + delta.X (and same for Y)
Im trying to make it follow the mouse while im moving it
ChangedConnection = UserInputService.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement then
if Holding == true then
local delta = UserInputService:GetMouseLocation()
local pos = UDim2.new(0, delta.X-item.AbsoluteSize.X/2, 0, delta.Y-item.AbsoluteSize.Y/2)
--TweenService:Create(item, TweenInfo.new(.2), {Position = UDim2.new(0, delta.X, 0, delta.Y)}):Play()
item.Position = pos
end
end
end)
If I understand correctly, when the user clicks a button, you want the button to follow the mouse position. To accomplish that, we can use the mouse position from the player:GetMouse() object.
local RunService = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
local button = script.Parent.TextButton
local following = false
local function follow_mouse()
while task.wait() do
local offset = Vector2.new(button.AbsoluteSize.X/2, button.AbsoluteSize.Y/2)
local new_position = UDim2.new(0, mouse.X - offset.X, 0, mouse.Y - offset.Y)
button.Position = new_position
end
end
local routine
button.MouseButton1Click:Connect(function()
following = not following
if following then
routine = coroutine.create(follow_mouse)
coroutine.resume(routine)
else
coroutine.close(routine)
end
end)
If you wanted a drag-like behaviour, it’d be a bit more difficult, but I am sure this is a good basis and you can research further using tools like chatgpt or youtube if necessary. Should my response have left open questions, feel free to either post them here or to send me a PM.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.