How can i make the camera to be locked on a certain axis?

So I’m making a 2D platformer game. But I’ve got something that I can’t solve.

I want the camera to be locked on the X and Y axis, but I don’t know how to do. There’s nothing wrong with the script. I don’t want the player to be able to move the camera at all.

Here’s the script:

local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local player = game.Players.LocalPlayer

player.CharacterAdded:Wait()

Camera.CameraType = Enum.CameraType.Attach
Camera.CameraSubject = player.Character:WaitForChild("HumanoidRootPart", 0.5)

RunService.Stepped:Connect(function()
    Camera.CFrame = CFrame.new(player.Character:WaitForChild("HumanoidRootPart", 0.5).Position) + CFrame.new(0,0,10)
end)

I would be really happy if someone could help.

Thanks for reading. :slight_smile:

local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local HRP = character:WaitForChild("HumanoidRootPart")

Camera.CameraType = Enum.CameraType.Attach
Camera.CameraSubject = player.Character:WaitForChild("HumanoidRootPart", 0.5)

RunService.Stepped:Connect(function()
	Camera.CFrame = CFrame.new(Camera.CFrame.X, Camera.CFrame.Y, HRP.CFrame.Z + 10)
end)

I don’t think you can “lock” the camera but you can control the camera through script. This tutorial includes a good section on how to do so How To Make Your Own 2D Game (NEW ASSET ADDED)

1 Like

This doesn’t work. At all. The player can move the camera everywhere.

It’s hard to tell what you were trying to achieve from the original post.

It works! Thank you so much! :grin::grin::grin: