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.

2 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?