I want to make a UI object move towards the mouse. Note, I DO NOT want it to teleport to the mouse. It needs to somewhat trail behind the mouse.
So once again, the object should gradually move towards my mouse. If I move my mouse while it’s moving towards it, it should change course to match the new position.
Ignore my kinda wacky/failed atttempt
while true do
local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
local CurrentPosition = script.Parent.Follow.AbsolutePosition
local Distance = (CurrentPosition - MouseLocation).Magnitude
local MoveToPosition
if CurrentPosition.X > MouseLocation.X and CurrentPosition.Y > MouseLocation.Y then
MoveToPosition = UDim2.fromOffset(
CurrentPosition.X - MouseLocation.X,
CurrentPosition.Y - MouseLocation.Y
)
elseif CurrentPosition.X < MouseLocation.X and CurrentPosition.Y > MouseLocation.Y then
MoveToPosition = UDim2.fromOffset(
MouseLocation.X - CurrentPosition.X,
CurrentPosition.Y - MouseLocation.Y
)
elseif CurrentPosition.X > MouseLocation.X and CurrentPosition.Y < MouseLocation.Y then
MoveToPosition = UDim2.fromOffset(
CurrentPosition.X - MouseLocation.X,
MouseLocation.Y - CurrentPosition.Y
)
else
MoveToPosition = UDim2.fromOffset(
MouseLocation.X - CurrentPosition.X,
MouseLocation.Y - CurrentPosition.Y
)
end
local TweenTo = TweenService:Create(
script.Parent.Follow,
TweenInfo.new(0.2),
{
Position = MoveToPosition
}
)
TweenTo:Play()
TweenTo.Completed:Wait()
end
Sorry that this is kinda bump, but for anybody that comes across this post in the future, there’s up to date method how to get Mouse.Move:
local UserInputService = game:GetService("UserInputService")
UserInputService.InputChanged:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
print("Mouse moved to position:",UserInputService:GetMouseLocation())
end
end)