For the current hours I’ve been trying to build a 2D camera that moves only on the Z axis. After a little bit of navigation on the Roblox documentation page I managed to create this script by myself:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local camera = game.Workspace.CurrentCamera
local plr = Players.LocalPlayer
local height = 2
local depth = 20
local function getRoot()
local char = plr.Character
local root = nil
if char then
root = char:FindFirstChild("HumanoidRootPart")
end
return root
end
task.spawn(function()
local function updateCamera()
while task.wait() do
local con = getRoot()
if con then
local rootPos = con.Position + Vector3.new(0, height, 0)
local cameraPos = Vector3.new(rootPos.X, rootPos.Y, depth)
camera.CFrame = CFrame.lookAt(rootPos, cameraPos)
end
end
end
RunService:BindToRenderStep("Focus", Enum.RenderPriority.Camera.Value + 1, updateCamera)
end)
The script that is on the Roblox documentation page works as the following sentence says. As soon as you spawn it locks 20 studs away from you on the Z axis. And if you try to get yourself in the opposite direction of the orientation, as soon as you reach 0 studs from the original position you start to glitch. The thing that I want is to constantly keep a 20 studs distance from the player’s HumanoidRootPart; not to lock it.
I’ve done everything in the upper script but when I try to move my mouse, I simply can’t. Test the script yourself if you want to know what I mean.
Anyway, here is the second script from the documentation page if you are interested:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local CAMERA_DEPTH = 24
local HEIGHT_OFFSET = 2
local function updateCamera()
local character = player.Character
if character then
local root = character:FindFirstChild("HumanoidRootPart")
if root then
local rootPosition = root.Position + Vector3.new(0, HEIGHT_OFFSET, 0)
local cameraPosition = Vector3.new(rootPosition.X, rootPosition.Y, CAMERA_DEPTH)
camera.CFrame = CFrame.lookAt(cameraPosition, rootPosition)
end
end
end
RunService:BindToRenderStep("SidescrollingCamera", Enum.RenderPriority.Camera.Value + 1, updateCamera)
I want to peacefully move my mouse while keeping a 20 studs distance from the character, that’s all. Any attempt of solution will be appreciated.