Object dragging system issue

So, i have this grabbing system, that allows you to pick up parts. And it almost works perfectly, but there’s this one issue that is very annoying.

So, when i pick up a part in first person, it works as intended. it doesn’t move from its original spot or anything, until i move it around of course.

But, if i pick it up in 3rd person, the object goes towards your camera, Which is REALLY annoying to navigate, I just need to NOT go towards your camera when you pick it up.

How would fix this?

local Player = game.Players.LocalPlayer
repeat wait() until Player.Character
local Char = Player.Character
local HRP = Char:WaitForChild("HumanoidRootPart")
local PlayerGui = Player:WaitForChild("PlayerGui")
local GrabGui = PlayerGui:WaitForChild("GrabGui")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local Mouse = Player:GetMouse() 
local Camera = workspace.CurrentCamera 

local Holding = false
local MaxGrabDistance = 10

local Object
local GrabbingForce
local GrabbingGyro

local ObjectDistance

-- Functions
function Grab()
	print("Grabbing")
	local BodyPos = Instance.new("BodyPosition")
	local BodyGyro = Instance.new("BodyGyro")
	BodyPos.D = 650
	BodyGyro.D = 650
	Object = Mouse.Target
	Object.CanGrab.Value = false
		
	GrabbingForce = BodyPos
	GrabbingGyro = BodyGyro
	
	GrabbingForce.Name = "GrabbingForce"
	GrabbingForce.Parent = Object

	GrabbingGyro.Name = "GrabbingGyro"
	GrabbingGyro.Parent = Object
	
	ObjectDistance = (Mouse.Target.Position - HRP.Position).Magnitude
	
	Holding = true
end

function Release()
	if Holding == true then
		print("Releasing")
		GrabbingForce:Destroy()
		GrabbingGyro:Destroy()
		Object.CanGrab.Value = true
		Object = nil
				
		Holding = false
	end
end

-- Start Grab
UIS.InputBegan:Connect(function(inputObject) 
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 and Mouse.Target and not Mouse.Target.Locked then
		local magnitude = (Mouse.Target.Position - HRP.Position).Magnitude
		local CanGrabValue = Mouse.Target:FindFirstChild("CanGrab")
		if Mouse.Target and not Mouse.Target.Locked and magnitude < MaxGrabDistance and not Holding and CanGrabValue and CanGrabValue.Value == true then
			Grab()
		else
			Release()
		end
	end
end)

-- End Grab
UIS.InputEnded:Connect(function(inputObject)
    if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		Release()
	end
end)

RS.RenderStepped:Connect(function()
	
	-- Grab GUI
	if Mouse.Target and not Mouse.Target.Locked then
		local magnitude = (Mouse.Target.Position - HRP.Position).Magnitude
		if magnitude < MaxGrabDistance and not Holding then
			local CanGrabValue = Mouse.Target:FindFirstChild("CanGrab")
			if CanGrabValue and CanGrabValue.Value == true then
				GrabGui.GrabText.Visible = true
			else
				GrabGui.GrabText.Visible = false
			end
		else
			GrabGui.GrabText.Visible = false
		end
	else
		GrabGui.GrabText.Visible = false
	end
	
	-- Grabbing
	if Holding and Object and Object:IsA("BasePart") then
		local magnitude = (Object.Position - HRP.Position).Magnitude
		if Holding and Object and GrabbingForce and GrabbingGyro and magnitude < (MaxGrabDistance + 5) then
			GrabbingForce.Position = Camera.CFrame.Position + (Mouse.UnitRay.Direction * (ObjectDistance - 1))
			GrabbingGyro.CFrame = Object.CFrame
		else
			Release()
		end
		-- Anti fly
		Object.Touched:Connect(function(hit)
			if hit.Parent and hit.Parent:FindFirstChild("Humanoid") and hit.Parent == Char then
				Release()
			end
		end)
	end
end)
6 Likes

Isn’t this your problem here? You’re setting the grabbing force’s position to the camera’s CFrame instead of the humanoidrootpart or whatever part of the character. So In first person it’d be fine because your camera is in the character, but farther out it’s not in the character.

Yeah, but doing that gives me a new problem.

The Object no longer goes to your mouse.

Maybe try raycasting from the mouse’s position in 6 directions, up, down, left, right, forward, back, and whichever raycast touches a part first The part goes 4 studs away from that position instead of a hardlocked 4 studs off your humanoidroot part or camera.

Iv’e managed to fix this issue!

I simply did this by adding distance from the player to the part, depending on your camera distance:

GrabbingForce.Position = Camera.CFrame.Position + (Mouse.UnitRay.Direction * (ObjectDistance + (workspace.CurrentCamera.CoordinateFrame.p - HRP.Position + Vector3.new(0, 1, 0)).magnitude - 2))