Rotate Camera With Mouse - Custom Camera Script

Hi there, here is my top-down camera script, of which the player moves around with the mouse.

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 = false -- locks mouse when dragging
local origin = CFrame.lookAt(Vector3.new(0,90,0), Vector3.new()) -- Where the camera will start
local moved = Vector2.zero -- How much the camera has moved

local dragging = false
local rotating = 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)

mouse.Button2Down:Connect(function()
	rotating = true
end)

mouse.Button2Up:Connect(function()
	rotating = false
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)

I’d like the use MouseButton2 to rotate the camera around, I’ve tried a lot of research and adding different CFrame values, nothing has worked. I’m sure it’s not too much more code. Please help. Cheers.

2 Likes