Adding limits to drag detector rotation

Hey,

Trying to make a 3 position momentary switch using drag detectors.
The idea being you have to hold it to keep the switch in that position else it goes back to the middle.

The issue I’m having is trying to set rotation limits, I want the player to be able to rotate the switch so far then it to stop without just making the player let go of it.

Currently I just have this to make it go back to the middle once released.

SwitchDrag.DragEnd:Connect(function()
	
	print("Drag Released")
	
	Switch.Orientation = Vector3.new(0,0,-80)
	
end)

Any advice on how to do this?
Thanks.

1 Like

Hello,
You want to create some rotation limit. I think the math.clamp() can be really useful here.

math.clamp() basically limits passed value in brackets.

Syntax: math.clamp(x, min, max)
Let’s some example:

local minNumber = 5; -- smallest number
local maxNumber = 25; -- biggest number

local x = 20
local c = math.clamp(x, minNumber, maxNumber); -- 'limiting it'

print(c); -- printing value (Output: 20);

So x is our value (for example x axis of some rotation). X is bigger than our minNumber and smaller than out maxNumber. So the script will print 20 in output, because is between the numbers we have already set.

Lets take this same piece of code, but let’s change x value for 35.

local minNumber = 5; -- smallest number
local maxNumber = 25; -- biggest number

local x = 35
local c = math.clamp(x, minNumber, maxNumber); -- 'limiting it'

print(c); -- printing value (Output: 25);

So output will print 25 (our max limit), because our x is greater than our maxNumber. The same goes with minNumber if our x is smaller than minNumber. If we’ll set x on let’s say 3 then script will print 5 because x crossed the limit we set earlier. It can be great tool for limiting some vectors and rotations.

I hope this isn’t too confusing.

There’s roblox documentation.

I hope this helps.

I did a similar lever type thing in my game, but I used a HingeConstraint for the movement.
You could just the hinge to Servo and put a small Force on it. A drag detector could overcome that force, but as soon as you release it it would go back to center.