How would I make a draggable lever?

Hello, everyone.

My question is: What is the concept or best approach I should take regarding making levers that could be dragged by the mouse?

-coolguyweir

3 Likes

Could you elaborate on what you mean by lever?

well, say we have a lever like this:
https://www.google.com/imgres?imgurl=https%3A%2F%2Fstatic.wikia.nocookie.net%2Fguilds%2Fimages%2F7%2F73%2FLeverofdoom08.gif%2Frevision%2Flatest%3Fcb%3D20120126103607&imgrefurl=https%3A%2F%2Fneopets.fandom.com%2Fwiki%2FLever_of_Doom&tbnid=IFjkQauv3JyyKM&vet=12ahUKEwjg94OWhLf2AhWPO80KHdXTBHsQMygFegUIARDkAQ..i&docid=MLFAsZEaxv0m5M&w=300&h=300&q=lever%20pull&client=safari&ved=2ahUKEwjg94OWhLf2AhWPO80KHdXTBHsQMygFegUIARDkAQ
image
How would I allow a user to drag it with the mouse?

Do you mean like a Gui or an actual 3d lever in the workspace?

Could you use a ClickDetector and a HingeConstraint set to Servo to move the lever, or do you need it to be actually moved with the mouse movement?

I want it to move with mouse movement. It’s literally as if the mouse is your hand.

I’m not sure about how to grab the lever with the mouse, but I’d suggest moving the lever with a HingeConstraint and the ActuatorType set to Servo.
I think you’d somehow have to transfer the Mouse.Target value to the Hinge servo TargetAngle value.

can you explain a bit more in detail on how I would transfer mouse.target to hinge servo targetangle?

local grabbing = false
on Mouse1 down
    if mouse.Target == Lever then
        grabbing = true


on Mouse1 up
    grabbing = false

local top = camera:WorldToScreenPoint(topOfLeverPanelVector3)
local bottom = camera:WorldToScreenPoint(bottomOfLeverPanelVector3)
while wait(1) do
    if not grabbing then continue end
    check if mouse.Y is in-between, above, or below the 2d positions (top & bottom)
    normalize mouse.Y for maxAngle and minAngle
    set servo angle to normalized angle
end
2 Likes

Hi! I mostly get your solution, but can you explain to me, in those last two lines, what “normalizing” means in terms of the angles?

If your lever’s max angle is 180 for all the way up and min angle 0 for all the way down then you just need to figure out how far from the bottom point that the mouse’s Y position is (as a percentage)

now if your mouse’s position is in the middle (50% in-between) then you do 180*.50 to get 90 deg (lever in the middle)

I was messing around with it but the math was off and I don’t want to spend too much time on it…
you can try to fix it yourself:
Lever.rbxl (33.5 KB)

this may also help: WorldToViewportPoint does not work as intended - Bug Reports / Engine Bugs - DevForum | Roblox