Make the Camera Draggable?

I will try this out tomorrow though, thanks for your time and effort!

1 Like

Hold on, I have a slightly different version from his which is a bit less complicated ill post it in about a minute or two, mines WILL also include smoothness!!

1 Like

bruh omg i finally finished after 2 hours haha :joy:, my first version worked but the dragging wasnt the way you wanted halfway through i was confused
@Wildcutepenguin
I did some research cause i never did this and realized i was missing the last position of where the mouse was!
Also its much simpler than the above one provided!

local player = game.Players.LocalPlayer
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()
camera.CameraType = Enum.CameraType.Scriptable

-- The height of the camera when player spawns in
local CAM_HEIGHT = 25
-- How fast it pawns around when your dragging!
local PAN_SPEED = 10
local lastDragPosition

-- Makes the camera look down
camera.CFrame = CFrame.new(0,CAM_HEIGHT,0) * CFrame.Angles(-math.rad(90),0,0)


mouse.Button1Down:Connect(function()
    dragging = true
    -- Remove the Y component of the Vector3 so it doesnt mess with the camera's Y pos
    lastDragPosition = Vector3.new(mouse.Hit.Position.X,0,mouse.Hit.Position.Z)
end)

mouse.Button1Up:Connect(function()
    dragging = false
end)

game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
    if dragging then
        local delta = Vector3.new(mouse.Hit.Position.X,0,mouse.Hit.Position.Z) - lastDragPosition
        camera.CFrame = CFrame.new(camera.CFrame.Position + -delta * deltaTime * PAN_SPEED) * CFrame.Angles(-math.rad(90),0,0)
    end
end)
1 Like

Absolutely perfect! I will definitely try this out tomorrow and then get back to you!

1 Like

Hey so I was looking over your script and tested it out and I noticed you used mouse.Hit which isn’t very good for right now because if you go near the edge, it’ll make you go flying. Would just mouse.X and mouse.Y work? I’m not sure if it would because the top left is 0 and the bottom right is 1000. Which means the middle would be 500. And also it’s x and y, not x and z. Sorry if that’s confusing but I’m wondering if you know how to make it work like this?

Oh I forgot to hit reply but it doesn’t matter anyway because I have a question. Do you think if I clamped the camera to a certain height then it wouldn’t ping everywhere?

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

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

local speed = 3 -- Speed when dragging
local lockMouse = true -- locks mouse when dragging
local origin = CFrame.lookAt(Vector3.new(0,10,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)

There are three variables in the top you can modify: speed, lockMouse, and origin. speed is the speed of movement when dragging, lockMouse is a boolean, if you want to lock the mouse in its position when dragging, and origin is the starting CFrame of the camera.
There is another variable called moved, it’s basically how much the camera has moved from the origin. You can modifiy also modify it, although I don’t think it’s necessary.

3 Likes

Works like a charm, I will modify it but thanks!

I was wondering if you’re able to clamp it so the camera can’t go outside a certain area?

1 Like

Yes, you can clamp it. And also, would you like it to be scrollable? (zoom in, zoom out)

Would you be able to clamp the zoom too? so like a minimum and maximum?

would i be able to put a limit so it cannot go outside a certain area?

Lielmaster helped me with that in pms, I can send you the final version of the script:

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

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local cam = workspace.Camera
cam.FieldOfView = 5

local clamp = math.clamp

local canDrag = script:GetAttribute("canDrag")
script.AttributeChanged:Connect(function()
	canDrag = script:GetAttribute("canDrag")
end)
local speed = 7 -- Speed when dragging
local lockMouse = false -- locks mouse when dragging
local origin = CFrame.lookAt(Vector3.new(0,300,0), Vector3.new()) -- Where the camera will start
local moveArea = Vector2.new(96,80) -- Clamp area

local zAngle = CFrame.Angles(0,0,math.rad(45))
local yAngle = CFrame.Angles(0, math.rad(0), 0)
local xAngle = CFrame.Angles(math.rad(45),0,0)

local scrollable = false -- Scrolling will zoom the camera
local scrollMin = 0 -- Scrolling minimum (studs)
local scrollMax = 100 -- Scrolling maximum (studs)
local scrollDelta = 2 -- How much movement when scrolling

local targetMoved = Vector2.zero -- How much the camera has moved
local targetScroll = clamp(10 ,scrollMin, scrollMax) -- How much the user has scrolled

local dragging = false
local oldMousePos = Vector2.zero
if uis.MouseEnabled then
	mouse.Button1Down:Connect(function()
		dragging = true
		oldMousePos = uis:GetMouseLocation()
	end)
	mouse.Button1Up:Connect(function()
		dragging = false
		uis.MouseBehavior = Enum.MouseBehavior.Default
	end)
	mouse.Move:Connect(function()
		if dragging then
			if lockMouse then
				uis.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
				targetMoved += uis:GetMouseDelta()*speed/60
			else
				targetMoved += (uis:GetMouseLocation() - oldMousePos)*speed/60
				oldMousePos = uis:GetMouseLocation()
			end
			targetMoved = Vector2.new(clamp(targetMoved.X, -moveArea.X, moveArea.X),clamp(targetMoved.Y, -moveArea.y, moveArea.Y))
		end
	end)
elseif uis.TouchEnabled then
	uis.InputBegan:Connect(function(input, gp)
		if gp then return end
		local pos = Vector2.new(input.Position.X,input.Position.Y)
		oldMousePos = pos
	end)
	uis.InputChanged:Connect(function(input)
		local pos = Vector2.new(input.Position.X,input.Position.Y)
		targetMoved += (pos - oldMousePos)*speed/60
		targetMoved = Vector2.new(clamp(targetMoved.X, -moveArea.X, moveArea.X),clamp(targetMoved.Y, -moveArea.y, moveArea.Y))
		oldMousePos = pos
	end)
end

local scroll = targetScroll
local moved = targetMoved
run.RenderStepped:Connect(function(dt)
	if not canDrag then return end
	scroll = scroll + (targetScroll - scroll)*dt*scrollDelta
	moved = moved:Lerp(targetMoved,dt*speed)
	cam.CFrame = origin * zAngle * CFrame.new(-moved.X, moved.Y, scroll) * xAngle * yAngle
end)

mouse.WheelForward:Connect(function()
	if scrollable then
		targetScroll = targetScroll - scrollDelta > scrollMin and targetScroll - scrollDelta or scrollMin
	end
end)
mouse.WheelBackward:Connect(function()
	if scrollable then
		targetScroll = targetScroll + scrollDelta > scrollMax and scrollMax or targetScroll + scrollDelta
	end
end)
1 Like

Thank you! It kind of works, all i wanna know is that, how would i make it so the camera can look all the way down, and not like it’s showing on the image:

I’ve been trying but i don’t get how to

nvm i fixed it, thank you!

(extra for the extra characters)