Need help with Mouse Delta replicating to mouse object X Y vectors

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)

Why not just use UserInputService | Roblox Creator Documentation then clamp that instead?

Because I cannot use the Mouse Object as I stated in here

unless if there is some way to disable the mouse from clicking on those core gui buttons which there is not I have no other choice but to do this

Why don’t you try disabling those CoreGuis while you are shooting and re-enable it once you are done.

1 Like

You cannot disable the Menu GUI which is my main concern, I will disable the others for sure though

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)

1 Like

unrelated but how did you make the camera move like that in the top video?
I’ve been looking for how to do that for a while now.