So there’s a few ways of doing this depending on what behaviour you’d want out of this script.
The DragDetector should work, if you utilise the TranslateLine
drag-style and the translation limits, though I don’t have much experience with it. Hopefully someone else can contribute some ideas. I’m aware that DragDetectors have a ‘Scriptable’ option which might be worth looking into, but again, I have no experience with it (sorry)
In terms of scripting it:
You could do it really simply, and just track the MousePosition.Y and translate it to a height value of the part between the minimum and maximum, but the position of the part won’t necessarily align with the mouse.
local MinimumHeight = 0;
local MaximumHeight = 5;
local MousePositionY = MousePosition.Y; -- Get from UserInputService or GetMouse() within a RenderStep function
local HeightOfPart = MinimumHeight + (MousePositionY / ViewFrameSize.Y) * (MaximumHeight - MinimumHeight)
If you want it to “follow” the mouse, you could utilise raycasts. An easy way would be to create an invisible wall along the plane you want the part to translate upward/downwards on, then use Camera:ScreenPointToRay
to determine a position. Extracting the Y-coordinate of the ray’s intersection point should give you the height value you’re looking for.
There is more complicated ways of doing it without the invisible wall, by doing a ScreenPointToRay
and then utilising the :Dot
product to determine the intersection point with the plane:
local d = planeNormal:Dot(planePosition - rayOrigin);
local n = planeNormal:Dot(rayDirection);
if (n == 0) then
return nil -- No intersection (parallel ray);
end
local t = d/n;
if (t < 0) then
return nil; -- Intersection is behind the ray origin
end
return rayOrigin +rayDirection *t;
But this seems unnecessarily complicated if you can just do the invisible wall.
math.clamp
on the resultant Y position will allow you to incorporate your upper and lower limits.
In terms of replicating to the server, you could experiment using SetNetworkOwnership and have the client handle the physics fully independently, or another option may be to use UnreliableRemoteEvents and update the position to the server regularly while it’s being dragged.