Make the Camera Draggable?

oh, if that’s the case, lock the camera, and you can use game.Workspace.currentcamera, something like this, lock the Y to a Y coord, then, you can use user input mouse to detect when mouse is held down, then do math behind the initial press, and the let go, to then move the X and Y coords.

:joy: I actually currently have a WASD camera move script and I’m trying to switch from that to using the mouse for a faster/smoother experience and compatibility. I did try to change it but it didn’t even get close to working thus why I created this topic.

Dont worry im still working on one that doesnt require a part and supports dragging with mouse

1 Like

Thank you so much you’re a live saver!

1 Like

Then why didn’t you specifiy that?

Hey so while looking around, I found this dev post which seems to have the solution you are looking for.

I have a video which shows how it should look:

I didn’t realise you were looking for a WASD script. Because at the time it wasn’t relevant.

Dang beat me too it, good work!

The post looks like what I want but in your video you just hold the mouse down and the camera moves by itself.

1 Like

You have to drag to make it move.

1 Like

yay in that case im still working on mine

1 Like

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?