Dragging Camera Limit

Hello developers, i am currently working on a few things, and one of them is basically a cam that moves by dragging the mouse, everything works fine so far, but i’m struggling with putting a limit so when it reaches a certain point, it stops moving.

Is there any way to do that? If so, your help would be appreciated :pray:

Here’s the script:

local run = game:GetService'RunService'
local uis = game:GetService'UserInputService'

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cam = workspace.CurrentCamera

local speed = 3 -- Speed when dragging
local lockMouse = true -- locks mouse when dragging
local origin = CFrame.lookAt(Vector3.new(0,1000,0), Vector3.new()) -- Where the camera will start
local moved = Vector2.zero -- How much the camera has moved

local dragging = false
local oldMousePos = Vector2.zero
mouse.Button1Down:Connect(function()
	dragging = true
	oldMousePos = uis:GetMouseLocation()
end)
mouse.Button1Up:Connect(function()
	dragging = false
	uis.MouseBehavior = Enum.MouseBehavior.Default
end)

run.RenderStepped:Connect(function(dt)
	if dragging then
		if lockMouse then
			uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			moved += uis:GetMouseDelta()*speed*dt
		else
			moved += (uis:GetMouseLocation() - oldMousePos)*speed*dt
			oldMousePos = uis:GetMouseLocation()
		end
	end
	
		cam.CFrame = origin * CFrame.new(-moved.X*speed, moved.Y*speed, 0)

end)

And here’s a video of how it works:

Also as an extra, if you could, is it possible to zoom in and out with the scroll of the mouse?

Greetings Nico.

You could look into using clamps to limit how far the player can drag the camera.

I would add zooming either by changing the camera FOV or moving it up and down while also changing the FOV, up to you

You can clamp the moved variable to your liking and for zooming do something like:

uis.InputChanged:Connect(function(i,gpe)
	if i.UserInputType == Enum.UserInputType.MouseWheel then
		distance  = math.clamp(distance-i.Position.Z*speed,100,1500)
	end
end)

Could you give me an example? I’m really trying to get how to do that :face_with_diagonal_mouth: