How do i make Custom 2D Draggable camera?

So i tried to make camera as the title said, but it goes like this:

It looks buggy… So how do i fix this?
Line of code that makes the camera part move:

local pos = getMouseTarget() -- 3D mouse Position
CamPart.Position = Vector3.new(-pos.X/2,CamPart.Position.Y,-pos.Z/2)
4 Likes

Hey, instead of moving the part, I’d just recommend move the client’s camera. Anyways, I am doing the same thing, and here is the code I developed to make it work.
Hope it helps!
Cheers :slight_smile:

local camera = workspace.CurrentCamera
local status = false
local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")


function onClick()
if not status then --if gui is open and change camera to overhead
	status = true
	workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = CFrame.new(Vector3.new(0,200,0), Vector3.new(0,0,0))
elseif status then --if gui closed and return camera to client
	local humanoid = player.Character:FindFirstChildOfClass("Humanoid")
	workspace.CurrentCamera.CameraSubject = humanoid
	workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	status = false
end	
end

script.Parent.TextButton.MouseButton1Click:Connect(onClick)
local mousedown = false
UserInputService.InputBegan:Connect(function(input)
local inputType = input.UserInputType
if inputType == Enum.UserInputType.MouseButton1 and status then
	mousedown = true
	UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition -- lock mouse to current position to get delta when pressed
end
end)

UserInputService.InputEnded:Connect(function(input)
local inputType = input.UserInputType
if inputType == Enum.UserInputType.MouseButton1 and status then
	mousedown = false
	UserInputService.MouseBehavior = Enum.MouseBehavior.Default -- return to default setting when mouse is latched
end
end)

RunService.RenderStepped:Connect(function()
if mousedown then
	local MousePosition = UserInputService:GetMouseDelta()
	camera.CFrame += Vector3.new(MousePosition.Y*0.25, 0, -1*MousePosition.X*0.25) --move camera depending on mouse delta
end
end)
1 Like