So, I was quite bored today, and I decided to make a quick little fish thingy. What I mean is like, whenever the player moves their mouse, the fish goes to that position, and if the mouse goes up, down, left, or right, the fish copies that. What I have right now looks a little buggy, though. I was wondering how I could improve it.
local uis = game:GetService('UserInputService')
local ts = game:GetService('TweenService')
local function rotate(angle)
local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
local tween = ts:Create(script.Parent, tweeninfo, {Rotation = angle})
tween:Play()
end
local frame = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local lastX = mouse.X
local lastY = mouse.Y
mouse.Move:Connect(function()
local x = mouse.X
local y = mouse.Y
local newPosition = UDim2.new(0, mouse.X, 0, mouse.Y)
frame.Position = newPosition
if x > lastX then
rotate(0)
elseif x < lastX then
rotate(180)
end
if y > lastY then
rotate(90)
elseif y < lastY then
rotate(-90)
end
lastX = x
lastY = y
end)
It can be improved by setting the dominant direction (in x or y) and canceling the current tween before doing the next one.
However it still shakes when the movement is diagonal (dominant movement interleaved). This is all that can be done with tweens (in my experience of course).
local uis = game:GetService('UserInputService')
local ts = game:GetService('TweenService')
local tween
local tweeninfo = TweenInfo.new(.1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false)
local function rotate(angle)
if tween then tween:Cancel() end
tween = ts:Create(script.Parent, tweeninfo, {Rotation = angle})
tween:Play()
end
local frame = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
local lastX = mouse.X
local lastY = mouse.Y
mouse.Move:Connect(function()
local x = mouse.X
local y = mouse.Y
local newPosition = UDim2.new(0, mouse.X, 0, mouse.Y)
frame.Position = newPosition
if math.abs(x - lastX) > math.abs(y - lastY) then
if x >= lastX then
rotate(0)
elseif x < lastX then
rotate(180)
end
else
if y >= lastY then
rotate(90)
elseif y < lastY then
rotate(-90)
end
end
lastX = x
lastY = y
end)