2D Camera

This tutorial will teach you how to turn your camera 2D.

by narwhiel

Author Avatar

12/21/2023

FULL SCRIPT AT THE BOTTOM:

Introduction

If you are looking to create a 2D platformer game but you don't know how to get the right camera, this one is for you.

First, create a local script in StarterPlayerScripts (game.StarterPlayer.StarterPlayerScripts) and name it "CameraScript", which is important because it will overwrite the old Camera Script and no longer follow the player, even without any code.

But if you run the game after only adding the CameraScript without any code, it will stay fixed at some random part of the map. The next part of this tutorial will fix that and turn it into what you actually asked for:

Script Now we're getting into the actual code, starting with the variables.

local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

These already seem pretty self-explanatory, so I'll skip the unnecessary explanation.

player.CharacterAdded:Wait()
player.Character:WaitForChild("HumanoidRootPart")

This part of the script is necessary, because without it the script will try to work without waiting for the character to fully load, in turn, it will fail.

HumanoidRootPart will be our camera's CameraSubject but it is optional and it can be any part of the player's Character.

The following now defines the camera:

camera.CameraSubject = player.Character.HumanoidRootPart
camera.CameraType = Enum.CameraType.Attach
camera.FieldOfView = 40

The first line of this segment sets the CameraSubject, what the camera is following/focused on.

Next is setting the CameraType to Attach, which means the camera will move with the subject at a fixed onset.

And lastly, setting FieldOfView to 40 changes the angle of the camera's vertical field of view.

game:GetService('RunService').Stepped:Connect(function()
	camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position) * CFrame.new(0,0,40)
end)

This is the last part of the script.

The first line of code will teach the camera to follow the player. Without it, it will stay at a fixed point somewhere in the map without moving and that's not exactly what we're aiming for.

After it is what sets the CFrame, this will define the position and orientation of the camera in the world.

Last thing to mention is always remember to put end at the end of all your statements.

That's all from this tutorial, thank you for reading and always remember to have fun first when dedicating to a game!


NOTE: Not everything in this script has to be exactly like the given. You can freely change HumanoidRootPart, FieldOfView and CFrame.

local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

player.CharacterAdded:Wait()
player.Character:WaitForChild("HumanoidRootPart")

camera.CameraSubject = player.Character.HumanoidRootPart
camera.CameraType = Enum.CameraType.Attach
camera.FieldOfView = 40

game:GetService('RunService').Stepped:Connect(function()
	camera.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position) * CFrame.new(0,0,40)
end)

View in-game to comment, award, and more!