I want to make parts with drag detectors draggable, even if they are inside other parts, without setting CanQuery to false.
Base off some preliminary research, I figured that collision groups might be the way to go. I set the drag detector’s DragStyle to scriptable, copied the script from a guide to scripting a drag detector, added the draggable part’s collision group to the raycast param’s CollisionGroup, and made that collision group unable to collide with the default collision group. That made the drag detector unable to be dragged at all, which was confusing, as the API mentioned, “Parts in collision groups that are set to not collide with [the set] group are ignored”. I haven’t tried many other things since, other than tinkering around with the other raycast param’s properties, none of which succeeded either.
Am I going the proper way with this? How do I get parts to be draggable through other parts?
I’ve been trying to find an answer for the past week and I’ve made a little bit of progress. The issue lies in the fact that the cursor ray taken in by the SetDragStyleFunction() method is the mouse’s cursor ray, which uses the default collision group. From what I could gather, this isn’t changeable either and I’ll either have to make a separate collision group that doesn’t collide with the default collision group for the covering parts, or script my own drag detectors in. I decided to go with the latter, as my only solution for ensuring that everything else collided with the covering part was to set them to another collision group that did collide with the covering part’s collision group.
In my efforts to do so, I’ve created a local script in StarterPlayerScripts. While I did manage to get the ray to use the correct collision group via the CollisionGroup property of raycast params, I’m now struggling to get the part to act like a proper drag detector with its DragStyle set to TranslateLine. I’ve gotten the part to be draggable even if the cursor stops hovering over the part, but now it’s acting erratically based on how far it is from the world origin and what position the mouse is on the screen (video file on Google Drive because the file size is too big to upload here).
I’ve got no idea what is causing that and would like some help figuring it out. Here’s the script that should be running it:
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local ray = nil
local raycast = nil
local dragPart = nil
local isDragging = false
RunService.RenderStepped:Connect(function(deltaTime)
-- Allows continuous dragging, even if player's mouse stops hovering over part
local mouseLocation = UserInputService:GetMouseLocation()
ray = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
end)
UserInputService.InputChanged:Connect(function(input, gameProcessedEvent)
-- The solution to getting parts to be draggable while covered by other parts
if input.UserInputType == Enum.UserInputType.MouseMovement then
local raycastParams = RaycastParams.new()
raycastParams.CollisionGroup = "DragPart"
raycast = workspace:Raycast(ray.Origin, ray.Direction * 25, raycastParams)
end
end)
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
--[[ Start dragging part ]]
if input.UserInputType == Enum.UserInputType.MouseButton1 and
raycast
then
isDragging = true
dragPart = raycast.Instance
while isDragging do
dragPart:PivotTo(CFrame.new(
ray:ClosestPoint(Vector3.new(dragPart.CFrame)).X,
dragPart.CFrame.Y,
dragPart.CFrame.Z))
task.wait()
end
end
end)
UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
--[[ Stop dragging part ]]
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isDragging = false
dragPart = nil
end
end)
I found a solution. It was to change ray:ClosestPoint(Vector3.new(dragPart.CFrame)).X to ray:ClosestPoint(dragPart.Position).X.
Why does it work? Heck if I know. Feel free to leave some guesses before this topic closes. Because while this problem is solved, I’m left not understanding the difference between cframe and position properties.