Top Down Camera Pan [Open Source]

This guy in scripting support needed help making a camera pan script, where you hold down MB1 and it pans around, so I had some trouble making it but eventually solved it!

I know this camera movement can probably be used in alot of games so, go make something great! I added documentation if you dont understand some stuff!

Also, I am aware i’m using mouse which is deprecated. just switch it out with userinputservice.

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)
11 Likes

Looks nice! Definitely going to be very helpful in my next couple games!

1 Like

Quick question do u know how to screenpointtoray camera system like the one in rise of nations?