Hey, its me again. I’ve got an issue with my “custom mouse” and so i’ll lay out the details…
So basically what I am trying to accomplish is a UI object that moves to the correct position as the mouse object normally would as in my game I need to avoid the players being able to click on core UI while shooting and such (video examples below). The only thing I could think of is using a UI object to mimic the mouse as the real mouse object is stuck in the center of the screen. If anyone knows a way how I can utilize Input.Delta better it will be very much appreciated!
Video of attempt:
Current Code:
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Frame = script.Parent
local Boundary = Frame.Parent.Boundary
local LastPosition
local function ClampFrame2(pos, frame)
print(LastPosition)
local NewPos = LastPosition + pos
local Size = frame.AbsoluteSize
local MinX = -(Size - frame.AbsolutePosition) + frame.AbsoluteSize
local MaxX = (Size + frame.AbsolutePosition) - Frame.AbsoluteSize
local X, Y = math.clamp(NewPos.X, MinX.X, MaxX.X), math.clamp(NewPos.Y, MinX.Y, MaxX.Y)
Frame.Position = UDim2.new(0, X, 0, Y)
if pos.X > 1 or pos.Y > 1 then
LastPosition = pos
end
end
RS.RenderStepped:Connect(function(Delta)
local Delta = UIS:GetMouseDelta()
if not LastPosition then LastPosition = Delta end
ClampFrame2(Delta,Boundary)
end)
Thanks for helping me in PMS, spoodr! Here is the fixed code for anyone who might stumble across this issue as well
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Frame = script.Parent
local Boundary = Frame.Parent.Boundary
local LastPosition
local function ClampFrame2(pos, frame)
print(LastPosition,pos)
local MousePos = Vector2.new(Mouse.X, Mouse.Y)
local NewPos = LastPosition + pos
local Size = frame.AbsoluteSize
local MinX = -(Size - frame.AbsolutePosition) + frame.AbsoluteSize
local MaxX = (Size + frame.AbsolutePosition) - Frame.AbsoluteSize
local X, Y = math.clamp(NewPos.X, MinX.X, MaxX.X), math.clamp(NewPos.Y, MinX.Y, MaxX.Y)
Frame.Position = UDim2.new(0, X, 0, Y)
LastPosition = Vector2.new(X,Y)
end
RS.RenderStepped:Connect(function(Delta)
local Delta = UIS:GetMouseDelta()
if not LastPosition then LastPosition = Vector2.new(Mouse.X, Mouse.Y) end
ClampFrame2(Delta,Boundary)
end)