I am fairly inexperienced so apologizes if I have missed something obvious. I have been trying to make a script in which the Text Button UI moves away from the player’s mouse.
I have frankensteined together some code in hopes it would work. Here was the result.(Audio warning)
Here is the code
local UserInputService = game:GetService("UserInputService")
local tweenservice = game:GetService("TweenService")
local Text = script.Parent
local TextPos = script.Parent.Position
local tween
UserInputService.InputChanged:Connect(function(inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseMovement then
MousePos = UserInputService:GetMouseLocation()
if (Text.AbsolutePosition - MousePos).Magnitude < 100 then
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
end
tween = tweenservice:Create(Text, TweenInfo.new(1),{
Position = UDim2.fromOffset(Text.AbsolutePosition.X+10, Text.AbsolutePosition.Y+10)
})
tween:Play()
else
if tween then
if tween.PlaybackState == Enum.PlaybackState.Playing then
tween:Cancel()
end
end
if Text.Position ~= TextPos then -- if position is not old position then
tween = tweenservice:Create(Text, TweenInfo.new(.3),{
Position = TextPos
}) -- Moving of the frame
tween:Play() -- playing the tween
end
end
end
end)
I have found other posts detail how to get the UI closer to the mouse. I tried tweaking the the already existing code to fit my need but ended up only making it kinda work.
All I need is just a little push in the right direction, but more help would be appreciated!
The AbsolutePosition property returns the top left corner of the UI object. In your code, your checking if the distance is less than 100 and for it to be less than 100, while coming from positions opposite (or perpendicular) to the top left corner, you will most probably need to put your mouse inside (or over if that makes more sense) the UI. You can fix this by:
Increasing the 100, not the best choice.
Getting the position from the middle using, AbsolutePosition + AbsoluteSize / 2, a better approach in my opinion.
Whenever I move my mouse towards the text button i wish for the text button to move away from the mouse. In the video it doesnt want to move away completely.
I see. Thank you for the info! Though i still have the issue of the text button not wanting to move away in some scenarios. Which ill try to fix in the time being. But your help is appreciated nevertheless!
Your welcome! Since the problem isn’t done yet, here are some suggestions you can do.
use a RenderStepped event instead of the InputChanged
1.5. No need to user TweenService anymore.
setting the magnitude to (√(sizeX^2 + sizeY^2)) / 2, divide by 2 as we start from the center. That is basically the maximum magnitude to the corners, after it, add n of your choice to control how far/close u should be from the button’s center for it t o move.
I’m still wrapping my head around this but basically I accidentally found a simpler way of doing this . Here is for anyone who is still stuck.
local RunService = game:GetService("RunService")
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local frame = script.Parent
RunService.Heartbeat:Connect(function()
local abs = frame.AbsolutePosition + frame.AbsoluteSize/2
local mouseP = Vector2.new(mouse.X, mouse.Y)
local Dist = (abs - mouseP)
local offset = Dist/100000
if Dist.Magnitude < 100 then
frame.Position = UDim2.new(frame.Position.X.Scale + offset.X ,0,frame.Position.Y.Scale + offset.Y,0)
print(offset.X)
end
end)
I thought I had to use trig to help me do this. I was very wrong.