How to set drag detector max drag range

the problem and failed solution

I already looked at this post but no results
Dragdetector max range?

i want it exactly like the example, but i cant replicate it no matter what i do

in the example, it is utilizing the AddConstraintFunction
i also want to use this function but i dont understand the example code

code i got so far
it sometimes work but, lots of bugs

local radius = 25
local startPartPosition = nil

dragDetector.DragStart:Connect(function(player, cursorRay)
	local root = player.Character.HumanoidRootPart
	dragDetector.ReferenceInstance = root
	startPartPosition = raycast(cursorRay, dragDetector, object).Position
end)

dragDetector.DragEnd:Connect(function()
	startPartPosition = nil
end)

--limits the drag range
dragDetector:AddConstraintFunction(1, function(proposedMotion) 
	return constraint(proposedMotion, dragDetector, radius, startPartPosition)
end)
return function(proposedMotion, dragDetector, radius, startPartPosition)
	if startPartPosition == nil then
		return proposedMotion
	end
	
	local worldMotion = dragDetector:GetReferenceFrame():VectorToWorldSpace(proposedMotion.Position)
	local newPartPosition = startPartPosition + worldMotion
	
	local center = dragDetector.ReferenceInstance.Position
	
	if (newPartPosition - center).Magnitude > radius then
		local dir = (newPartPosition - center).Unit
		newPartPosition = center + dir * radius
		local newWorldTranslation = newPartPosition - startPartPosition
		local newRefSpaceTranslation = dragDetector:GetReferenceFrame():VectorToObjectSpace(newWorldTranslation)
		return proposedMotion.Rotation + newRefSpaceTranslation
	else 
		return proposedMotion  
	end
end

so far i got this, but im not satisfied with the results its quite buggy
by glitching, i mean it goes through the ground, it hovers in the air (aka the drop shadow)
it also ignores the raycasting and goes through parts when walking through

-here if you wanna test what i got so far
pizza constraint.rbxm (7.9 KB)-

new test place
block.rbxl (68.4 KB)

in action

explanation


so first the blue is the position of the character,
next position of the red is where the mouse is

the yellow (red and green combined) is within the range
or if outside the range the green position is 25 studs from the blue position to red position

and lastly another raycast from the camera to the green position

one slight issue

if you back up while facing the wall while holding the yellow disk, it’ll go beyond the max range

2 Likes